You are viewing a plain text version of this content. The canonical link for it is here.
Posted to httpclient-users@hc.apache.org by BrianP <Br...@firstdatabank.com> on 2008/10/24 21:07:52 UTC

how to get redirect url from response with HttpClient 4?

I've got a basic connection and request/response working with HttpClient. 
But it appears to be redirecting, and I need that redirected url because I
need to post to it.  Where/how can I get that with the org.apache.http.foo
classes?  I've been digging around in the docs for awhile and can't find it. 
I thought there was a location header or response url header, but I've
logged the headers and the url isn't there either.

For example, I'm doing my first request to http://myservice.com and it's
responding with
http://myservice.com/(S(jaaxb545nlp3p455mfcz2qbm))/index.aspx. 

Here's my code:

HttpClient client = new DefaultHttpClient();
HttpGet getMethod = new HttpGet("http://myservice.com");
try {
    HttpResponse response = client.execute(getMethod);
    String res = EntityUtils.toString(response.getEntity());
    String viewState = extractParm(res, "__VIEWSTATE");
    log(viewState);
   
    Header[] headers = response.getAllHeaders();
    for(Header header : headers) {
        log(header.getName() + " : " + header.getValue());
    }
           
    // getting ready for post
    List<NameValuePair> pairs = new ArrayList<NameValuePair>();
    pairs.add(new BasicNameValuePair("__VIEWSTATE", viewState));

} catch (Exception e) {
    e.printStackTrace();
    log(e.getMessage());
}

Thanks
-- 
View this message in context: http://www.nabble.com/how-to-get-redirect-url-from-response-with-HttpClient-4--tp20156112p20156112.html
Sent from the HttpClient-User mailing list archive at Nabble.com.


---------------------------------------------------------------------
To unsubscribe, e-mail: httpclient-users-unsubscribe@hc.apache.org
For additional commands, e-mail: httpclient-users-help@hc.apache.org


Re: how to get redirect url from response with HttpClient 4?

Posted by Joseph Mocker <mo...@fakebelieve.org>.
You can handle the redirect handling yourself with the 
HttpClientParams.setRedirecting(). Set redirecting to false.

You will then start to see 301 & 302 status codes in 
response.getStatusLine().getStatusCode().

When you do, process the headers to look for the Location: header and 
restart your request.

  --joe


BrianP wrote:
> I've got a basic connection and request/response working with HttpClient. 
> But it appears to be redirecting, and I need that redirected url because I
> need to post to it.  Where/how can I get that with the org.apache.http.foo
> classes?  I've been digging around in the docs for awhile and can't find it. 
> I thought there was a location header or response url header, but I've
> logged the headers and the url isn't there either.
>
> For example, I'm doing my first request to http://myservice.com and it's
> responding with
> http://myservice.com/(S(jaaxb545nlp3p455mfcz2qbm))/index.aspx. 
>
> Here's my code:
>
> HttpClient client = new DefaultHttpClient();
> HttpGet getMethod = new HttpGet("http://myservice.com");
> try {
>     HttpResponse response = client.execute(getMethod);
>     String res = EntityUtils.toString(response.getEntity());
>     String viewState = extractParm(res, "__VIEWSTATE");
>     log(viewState);
>    
>     Header[] headers = response.getAllHeaders();
>     for(Header header : headers) {
>         log(header.getName() + " : " + header.getValue());
>     }
>            
>     // getting ready for post
>     List<NameValuePair> pairs = new ArrayList<NameValuePair>();
>     pairs.add(new BasicNameValuePair("__VIEWSTATE", viewState));
>
> } catch (Exception e) {
>     e.printStackTrace();
>     log(e.getMessage());
> }
>
> Thanks
>   


---------------------------------------------------------------------
To unsubscribe, e-mail: httpclient-users-unsubscribe@hc.apache.org
For additional commands, e-mail: httpclient-users-help@hc.apache.org


Re: how to get redirect url from response with HttpClient 4?

Posted by Christine <ch...@christine.nl>.
On Fri, 2008-10-24 at 12:07 -0700, BrianP wrote:
> I've got a basic connection and request/response working with HttpClient. 
> But it appears to be redirecting, and I need that redirected url because I
> need to post to it. 

As far as I know, HttpClient automatically follows the redirect. If it's
a client side redirect using a refresh tag, you'll have to parse the
html and find the proper url there.




>  Where/how can I get that with the org.apache.http.foo
> classes?  I've been digging around in the docs for awhile and can't find it. 
> I thought there was a location header or response url header, but I've
> logged the headers and the url isn't there either.
> 
> For example, I'm doing my first request to http://myservice.com and it's
> responding with
> http://myservice.com/(S(jaaxb545nlp3p455mfcz2qbm))/index.aspx. 
> 
> Here's my code:
> 
> HttpClient client = new DefaultHttpClient();
> HttpGet getMethod = new HttpGet("http://myservice.com");
> try {
>     HttpResponse response = client.execute(getMethod);
>     String res = EntityUtils.toString(response.getEntity());
>     String viewState = extractParm(res, "__VIEWSTATE");
>     log(viewState);
>    
>     Header[] headers = response.getAllHeaders();
>     for(Header header : headers) {
>         log(header.getName() + " : " + header.getValue());
>     }
>            
>     // getting ready for post
>     List<NameValuePair> pairs = new ArrayList<NameValuePair>();
>     pairs.add(new BasicNameValuePair("__VIEWSTATE", viewState));
> 
> } catch (Exception e) {
>     e.printStackTrace();
>     log(e.getMessage());
> }
> 
> Thanks
-- 
dagdag is just a two character rotation of byebye
www.christine.nl


---------------------------------------------------------------------
To unsubscribe, e-mail: httpclient-users-unsubscribe@hc.apache.org
For additional commands, e-mail: httpclient-users-help@hc.apache.org