hc-httpclient-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Oleg Kalnichevski <ol...@apache.org>
Subject Re: Trying to redirect after post
Date Thu, 15 May 2008 19:00:55 GMT
On Tue, 2008-05-13 at 08:28 -0500, Kara Burkhalter wrote:
> Hi,
> 
> I am a newbie to HttpClient and I am hoping someone can help me out.  I 
> haven't been able to get this to work and I don't know why.  I am trying 
> to post to a form at a URL that has the following fields:  agent_id, 
> password, poltyp, polno, and submit button.  I get a status code of 200 
> back instead of a redirect response code.  I'm not sure why this is 
> happening.  Am I missing something in my code, did I do something wrong, 
> etc?  Any help is greatly appreciated.  I am trying to get this done 
> before I have a baby in the next few weeks. 
> 
> Thank You!
> Kara
> 

Kara,

There is simply no easy way of telling. Each server-side script can be
different.

(1) Please make sure you have read this:

http://wiki.apache.org/HttpComponents/ForAbsoluteBeginners

(2) Make sure you are using the browser compatibility cookie policy.
There are many web sites with badly written CGI scripts that only work
when all cookies are put into one request header. Set
http.protocol.single-cookie-header parameter to true for maximum
compatibility

http://hc.apache.org/httpclient-3.x/cookies.html#Browser%20Compatibility

(3) You may want to capture HTTP packets generated by a browser using a
traffic analyzer or a local proxy and then simulate the session with
HttpClient.

Hope this helps somewhat.

Oleg




> Here is my code:
> 
>                         // Create an instance of HttpClient.
>                         HttpClient client = new HttpClient();
>                         HttpServletResponse response = null;
>  
>                         // Create a method instance.
>                         PostMethod post = new PostMethod(
>                                         
> "https://www.wiins.com/admin/cgi-bin/test/policy_navigation.mac/service_link"
> );
> 
>                         NameValuePair[] data = { new NameValuePair(
> "agent_id", username),
>                                         new NameValuePair("password", pwd
> ),
>                                         new NameValuePair("poltyp", type),
>                                         new NameValuePair("polno", policy)
>                                          };
> 
>  
>                         post.setRequestBody(data);
>  
>                         client.executeMethod(post);
>                         post.releaseConnection();
>                         int statuscode = post.getStatusCode();
>                         System.out.println("Statuscode = " + statuscode);
>  
>                         if ((statuscode == HttpStatus.SC_MOVED_TEMPORARILY
> )
>                                         || (statuscode == HttpStatus.
> SC_MOVED_PERMANENTLY)
>                                         || (statuscode == HttpStatus.
> SC_SEE_OTHER)
>                                         || (statuscode == HttpStatus.
> SC_TEMPORARY_REDIRECT)) {
>                                 Header header = post.getResponseHeader(
> "Location");
>  
>                                 System.out.println("location=" + 
> header.getValue());
>  
>                                 if (header != null) {
>                                         String newuri = header.getValue();
> 
>                                         if ((newuri == null) || 
> (newuri.equals(""))) {
>                                                 newuri = "/";
>                                         }
> 
>                                         GetMethod redirect = new 
> GetMethod(newuri);
>                                         client.executeMethod(redirect);
> 
>                                         if (newuri.indexOf("jsessionid=") 
> > -1) {
>                                                 String jsessionid = 
> newuri.substring(newuri
>                                                                 .indexOf(
> "jsessionid=") + 11, newuri.length());
> 
>  
> 
>                                                 response.addHeader(
> "Set-Cookie", "JSESSIONID="
>                                                                 + 
> jsessionid + ";Path="
>                                                                 + 
> request.getContextPath());
>                                         }
>  response.sendRedirect(newuri);
>  
>                                 }
>                         }
> 
> 
> This message may contain confidential or proprietary information and is 
> intended only for the person(s) to whom it is addressed. Any use, 
> distribution, copying or disclosure of confidential information by any 
> other person is strictly prohibited. If you have received this message in 
> error, please notify the e-mail sender immediately, and delete the 
> original message without making a copy. For information on the UW 
> Foundation's privacy policy, please visit:
> UW Foundation Privacy Policy


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


Mime
View raw message