Any help is greatly appreciated! I need to get this working before I have
this baby in a few weeks. I am trying to send data to a form using
HttpClient post method. I can sucessfully get there but I am not being
redirected. I am getting a status code of 200 instead of one in the 300s.
Do I need to put the actual submit button in the name value pair for the
post to get it to work? Am I missing something else? I'm not sure if its
my code or the URL I am going against. I know the URL will come up and if
I enter the data into the form and click submit it does redirect to the
new URL.
Thanks!
// Create an instance of HttpClient.
HttpClient client = new
HttpClient();
HttpServletResponse
response = null;
// Create a method
instance.
PostMethod post = new
PostMethod(
"
https://URL");
NameValuePair[] data = {
new NameValuePair("agent_id", username),
new NameValuePair("password", pwd),
new NameValuePair("poltyp", type),
new NameValuePair("polno", policy) };
//
post.setFollowRedirects(true);
post.setRequestBody(data);
//
System.out.println("requestBody =" + data.toString());
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
As an Amazon Associate we earn from qualifying purchases.