Obtaining URL from http response when no location header is sent - java

When communicating with http to http://forecast.weather.gov/zipcity.php I need to obtain the URL that is generated from a request.
I have printed out the headers and their values from the http response message but there is no location header. How can I obtain this URL? (I'm using HttpClient)

It should be similar to:
HttpClient client = new DefaultHttpClient();
HttpParams params = client.getParams();
HttpClientParams.setRedirecting(params, false);
HttpGet method = new HttpGet("http://forecast.weather.gov/zipcity.php?inputstring=90210");
HttpResponse resp = client.execute(method);
String location = resp.getLastHeader("Location").getValue();
EDIT: I had to make a couple minor tweaks, but I tested and the above works.

Related

Google authorization access API via browser

I have a problem with my Google API.
I would like to avoid going through a browser to retrieve my access authorization.
I would like to retrieve the code directly into a variable.
This is the kind of URL that I sent :
https://accounts.google.com/o/oauth2/auth?response_type=code&client_id=2131233123321332&scope=https://www.googleapis.com/auth/admin.directory.group%20https://www.googleapis.com/auth/admin.directory.group.readonly&redirect_uri=https://tito.com
HttpClient httpclient = new DefaultHttpClient();
HttpGet httpget = new HttpGet("https://accounts.google.com/o/oauth2/auth?access_type=online&approval_prompt=auto&client_id=22fdgdfg40-1gmgdfggdps0hfol.apps.googleusercontent.com&redirect_uri=https://tito.com/&response_type=code&scope=https://www.googleapis.com/auth/admin.directory.group%20https://www.googleapis.com/auth/admin.directory.group.readonly");
HttpResponse responses = client.execute(httpget);
HttpParams param = responses.getParams();
responses.getParams().getParameter("code");
System.out.println(responses.getParams().getParameter("code"));
System.out.println("Response Code : " + responses.getStatusLine().getStatusCode());

Why am I getting last response if my server doesn't work?

I use simple code to get XML content.
but I have a trouble if my server doesn't work, I get last success response.
I tried all methods:
send every time another URI
setHeader Cache-Content How to prevent Android from returning a cached response to my HTTP Request?
I tried even HttpURLConnection with GET.
but nothing helps
DefaultHttpClient client = new DefaultHttpClient();
String fullPath = path + name;
HttpGet request = new HttpGet(fullPath);
HttpResponse response = client.execute(request);
HttpEntity entity = response.getEntity();
InputStream is = entity.getContent();
//....decode input string

Get response Uri parameters from apache HttpResponse

I'm sending an http get/head request using Apache HttpClient 4.x. I'm sending a request with a url like "http://example.com/getAccessToken". I'm expecting the response to be a redirect url with parameters in the returned url like "http://redirecturl.com/?code=accessTokenStuff". I want to be able to parse the response redirect url parameters, i.e. I want to get "accessTokenStuff". How can I do that?
HttpClient client = new DefaultHttpClient();
HttpHead request = new HttpHead(authUrl);
HttpResponse response = client.execute(request);
System.out.println(response.getStatusLine().getStatusCode());//returns 200
request.releaseConnection();
In a nutshell: what I want is executing an original url and then getting the result which is another url that has a parameter called "code". Then I want to get the value of that parameter.
EDIT:
I also tried this but it returns the same original URL
DefaultHttpClient client = new DefaultHttpClient();
HttpParams params = client.getParams();
HttpClientParams.setRedirecting(params, false);
HttpGet request = new HttpGet(authUrl);
HttpResponse response = client.execute(request);
String location = response.getLastHeader("Location").getValue();//returns same original url
System.out.println(location);
request.releaseConnection();
Setting HttpClientParams.setRedirecting(params, true); return null
Http response do not take a form of a "redirect url". Redirect and response are both different (but related) concepts. Redirect usually means "get your response from this address instead of original one".
Having said this, you can prevent HttpClient from following a redirect, see this answer: How to prevent apache http client from following a redirect
When your HttpClient is not following the redirect, you can inspect the 'Location:' header of its response, eg:
HeaderIterator iterator = httpResponse.headerIterator("Location");
while(iterator.hasNext()) {
Header header = iterator.nextHeader();
String redirectUrl = header.getValue();
}

Getting Exception while calling soap webservice

When I am using SoapUI to call this web service I am getting the correct response but when I implement this in android, I am getting the below exception,
system.web.services.protocols.soapheaderexception (some information is missing).
This is what I tried,
HttpPost httppost = new HttpPost("http://www.ocrwebservice.com/services/OCRWebService.asmx");
StringEntity se = new StringEntity(SOAPRequestXML,HTTP.UTF_8);
se.setContentType("text/xml");
httppost.setHeader("Content-Type", "text/xml;charset=UTF-8");
httppost.setEntity(se);
HttpClient httpclient = new DefaultHttpClient();
BasicHttpResponse httpResponse =
(BasicHttpResponse) httpclient.execute(httppost);
HttpEntity resEntity = httpResponse.getEntity();
I tried other combinations also like,
httppost.setHeader("Accept-Charset","utf-8")
and
httppost.setHeader("Content-Type","application/soap+xml;charset=UTF-8")
But nothing worked.
The error says,
System.Web.Services.Protocols.SoapHeaderException: WSE012: The input was not a valid SOAP message because the following information is missing: action.
System.Web.Services.Protocols.SoapHeaderException: WSE012: The input
was not a valid SOAP message because the following information is
missing: action.
=> As per the above exception, I can say you forgot to set Action.
Try:
String SOAP_ACTION = "http://stockservice.contoso.com/wse/samples/2005/10/OCRWebServiceAvailablePages";
httppost.setHeader("SOAPAction", SOAP_ACTION);

Multiple GET requests to a single site (canonical) (java)

Hi I am trying to make 2 GET requests to a single connection. ie
HttpGet get1 = new HttpGet("http://www.google.com/search?q=HelloWorld");
HttpGet get2 = new HttpGet("http://www.google.com/search?q=SecondSearch");
HttpResponse response = null;
response = client.execute(get1);
response = client.execute(get2);
I would like to get the body from the second execution. Obviously this fails, because it says you must release the connection first. I need to maintain the exact session - for instance, if I navigate to a site where the first step is to login, I need to navigate to any subsequent pages with the same cookie.
It's probably something incredibly simple that I am doing wrong!
You need to use a CookieStore
CookieStore cookieStore = new BasicCookieStore();
DefaultHttpClient client1 = new DefaultHttpClient();
client1.setCookieStore(cookieStore);
HttpGet httpGet1 = new HttpGet("...");
HttpResponse response1 = client1.execute(httpGet1);
DefaultHttpClient client2 = new DefaultHttpClient();
client2.setCookieStore(cookieStore);
HttpGet httpGet2 = new HttpGet("...");
HttpResponse response2 = client2.execute(httpGet2);
In the above code, both client2 will re-use cookies from the client1 request.

Categories

Resources