|
-----Original Message-----
From: java400-l-bounces@xxxxxxxxxxxx [mailto:java400-l-
bounces@xxxxxxxxxxxx] On Behalf Of Kara Burkhalter
Sent: Monday, May 12, 2008 7:50 AM
To: java400-l@xxxxxxxxxxxx
Subject: redirecting from httpclient post
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
--
This is the Java Programming on and around the iSeries / AS400
(JAVA400-L) mailing list
To post a message email: JAVA400-L@xxxxxxxxxxxx
To subscribe, unsubscribe, or change list options,
visit: http://lists.midrange.com/mailman/listinfo/java400-l
or email: JAVA400-L-request@xxxxxxxxxxxx
Before posting, please take a moment to review the archives
at http://archive.midrange.com/java400-l.
As an Amazon Associate we earn from qualifying purchases.
This mailing list archive is Copyright 1997-2024 by midrange.com and David Gibbs as a compilation work. Use of the archive is restricted to research of a business or technical nature. Any other uses are prohibited. Full details are available on our policy page. If you have questions about this, please contact [javascript protected email address].
Operating expenses for this site are earned using the Amazon Associate program and Google Adsense.