hc-httpclient-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Kara Burkhalter <Kara.Burkhal...@uwfoundation.wisc.edu>
Subject Trying to redirect after post
Date Tue, 13 May 2008 13:28:51 GMT
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

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

Mime
  • Unnamed multipart/alternative (inline, None, 0 bytes)
View raw message