redirect issue with httpclient
hi all, I am using Jakarta HTTPClient to do a post on a website. it seems that after the authentication the page is getting redirected to other location. the significant thing is that its changing the port also. below i am posting the output i am getting from my code. May 13, 2004 12:43:25 PM org.apache.commons.httpclient.HttpMethodBase readResponseBody WARNING: Response content length is not known May 13, 2004 12:43:29 PM org.apache.commons.httpclient.HttpMethodBase readResponseBody WARNING: Response content length is not known May 13, 2004 12:43:29 PM org.apache.commons.httpclient.HttpMethodBase processRedirectResponse INFO: Redirect requested but followRedirects is disabled STATUS CODE = 302 May 13, 2004 12:43:32 PM org.apache.commons.httpclient.HttpMethodBase readResponseBody WARNING: Response content length is not known May 13, 2004 12:43:32 PM org.apache.commons.httpclient.HttpMethodBase processRedirectResponse WARNING: Redirect from port 2048 to 2096 is not supported* * how can i make httpclient to get redirected to the new location automatically. here is my code: import org.apache.commons.httpclient.Cookie; import org.apache.commons.httpclient.cookie.CookiePolicy; import org.apache.commons.httpclient.Header; import org.apache.commons.httpclient.HttpClient; import org.apache.commons.httpclient.HttpStatus; import org.apache.commons.httpclient.NameValuePair; import org.apache.commons.httpclient.methods.GetMethod; import org.apache.commons.httpclient.methods.PostMethod; public class FormLoginDemo { static final String LOGON_SITE = www.somedomain.com; static final intLOGON_PORT = 2048; static final String PROXY_HOST = 192.168.10.1; static final int PROXY_PORT = 80; public static void main(String[] args) throws Exception { HttpClient client = new HttpClient(); client.getHostConfiguration().setHost(LOGON_SITE, LOGON_PORT, http); client.getHostConfiguration().setProxy(PROXY_HOST,PROXY_PORT); client.getState().setCookiePolicy(CookiePolicy.COMPATIBILITY); GetMethod authget = new GetMethod(/login); client.executeMethod(authget); Header location = authget.getResponseHeader(Location); authget.releaseConnection(); PostMethod authpost= new PostMethod(/login); authpost.setFollowRedirects(true); NameValuePair action = new NameValuePair(action, login); NameValuePair url = new NameValuePair(url, someurl); NameValuePair userid = new NameValuePair(user, myuser); NameValuePair password = new NameValuePair(pass, mypassword); authpost.setRequestBody( new NameValuePair[] {action, url, userid, password}); client.executeMethod(authpost); authpost.releaseConnection(); int statuscode = authpost.getStatusCode(); System.out.println(STATUS CODE = +statuscode); if ((statuscode == HttpStatus.SC_MOVED_TEMPORARILY) || (statuscode == HttpStatus.SC_MOVED_PERMANENTLY) || (statuscode == HttpStatus.SC_SEE_OTHER) || (statuscode == HttpStatus.SC_TEMPORARY_REDIRECT)) { Header header = authpost.getResponseHeader(location); if (header != null) { String newuri = header.getValue(); if ((newuri == null) || (newuri.equals())) newuri = /; GetMethod redirect = new GetMethod(newuri); client.executeMethod(redirect); redirect.releaseConnection(); } else { System.out.println(Invalid redirect); System.exit(1); } } } } any help in this context will be highly appreciated. Regards. Himanshu. - To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
RE: redirect issue with httpclient
Himanshu, HttpClient 2.0, unfortunately, cannot automatically handle cross-host redirects. But this limitation is not difficult to work around. See the document below http://jakarta.apache.org/commons/httpclient/redirects.html HttpClient 3.0 will address this limitation Oleg -Original Message- From: Himanshu Pathak [mailto:[EMAIL PROTECTED] Sent: Thursday, May 13, 2004 10:08 To: [EMAIL PROTECTED] Subject: redirect issue with httpclient hi all, I am using Jakarta HTTPClient to do a post on a website. it seems that after the authentication the page is getting redirected to other location. the significant thing is that its changing the port also. below i am posting the output i am getting from my code. May 13, 2004 12:43:25 PM org.apache.commons.httpclient.HttpMethodBase readResponseBody WARNING: Response content length is not known May 13, 2004 12:43:29 PM org.apache.commons.httpclient.HttpMethodBase readResponseBody WARNING: Response content length is not known May 13, 2004 12:43:29 PM org.apache.commons.httpclient.HttpMethodBase processRedirectResponse INFO: Redirect requested but followRedirects is disabled STATUS CODE = 302 May 13, 2004 12:43:32 PM org.apache.commons.httpclient.HttpMethodBase readResponseBody WARNING: Response content length is not known May 13, 2004 12:43:32 PM org.apache.commons.httpclient.HttpMethodBase processRedirectResponse WARNING: Redirect from port 2048 to 2096 is not supported* * how can i make httpclient to get redirected to the new location automatically. here is my code: import org.apache.commons.httpclient.Cookie; import org.apache.commons.httpclient.cookie.CookiePolicy; import org.apache.commons.httpclient.Header; import org.apache.commons.httpclient.HttpClient; import org.apache.commons.httpclient.HttpStatus; import org.apache.commons.httpclient.NameValuePair; import org.apache.commons.httpclient.methods.GetMethod; import org.apache.commons.httpclient.methods.PostMethod; public class FormLoginDemo { static final String LOGON_SITE = www.somedomain.com; static final intLOGON_PORT = 2048; static final String PROXY_HOST = 192.168.10.1; static final int PROXY_PORT = 80; public static void main(String[] args) throws Exception { HttpClient client = new HttpClient(); client.getHostConfiguration().setHost(LOGON_SITE, LOGON_PORT, http); client.getHostConfiguration().setProxy(PROXY_HOST,PROXY_PORT); client.getState().setCookiePolicy(CookiePolicy.COMPATIBILITY); GetMethod authget = new GetMethod(/login); client.executeMethod(authget); Header location = authget.getResponseHeader(Location); authget.releaseConnection(); PostMethod authpost= new PostMethod(/login); authpost.setFollowRedirects(true); NameValuePair action = new NameValuePair(action, login); NameValuePair url = new NameValuePair(url, someurl); NameValuePair userid = new NameValuePair(user, myuser); NameValuePair password = new NameValuePair(pass, mypassword); authpost.setRequestBody( new NameValuePair[] {action, url, userid, password}); client.executeMethod(authpost); authpost.releaseConnection(); int statuscode = authpost.getStatusCode(); System.out.println(STATUS CODE = +statuscode); if ((statuscode == HttpStatus.SC_MOVED_TEMPORARILY) || (statuscode == HttpStatus.SC_MOVED_PERMANENTLY) || (statuscode == HttpStatus.SC_SEE_OTHER) || (statuscode == HttpStatus.SC_TEMPORARY_REDIRECT)) { Header header = authpost.getResponseHeader(location); if (header != null) { String newuri = header.getValue(); if ((newuri == null) || (newuri.equals())) newuri = /; GetMethod redirect = new GetMethod(newuri); client.executeMethod(redirect); redirect.releaseConnection(); } else { System.out.println(Invalid redirect); System.exit(1); } } } } any help in this context will be highly appreciated. Regards. Himanshu. - To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] *** The information in this email is confidential and may be legally privileged. Access to this email by anyone other than the intended addressee is unauthorized. If you are not the intended recipient of this message, any review, disclosure, copying, distribution, retention, or any action taken or omitted to be taken in reliance on it is prohibited and may be unlawful. If you are not the intended recipient, please reply
Re: Redirect Issue
Use the logging features to see exactly what was sent back (and make sure you're cookies are in compatibility mode). You can find more info on logging at http://jakarta.apache.org/commons/httpclient/logging.html Cookies are generally Oleg's realm but I'd pretty much guarantee he'll need a wire trace log to track down the problem. :) Regards, Adrian Sutton. On Wednesday, May 28, 2003, at 03:44 PM, Sunil Kumar K wrote: Thanks Oleg, I tried that, now I'm getting this error.. WARNING: Invalid cookie header: DOMS=; PATH=. Missing value for path attribute What should I do...?? regards sunil - Original Message - From: Kalnichevski, Oleg [EMAIL PROTECTED] To: Commons HttpClient Project [EMAIL PROTECTED]; Sunil Kumar Kolar [EMAIL PROTECTED] Sent: Tuesday, May 27, 2003 8:52 PM Subject: RE: Redirect Issue Sunil, At the moment HttpClient does not support automatic POST redirects. All you have to do it is to manually issue GET request to the location specified in the redirect response. Have a look at the following resource for more details http://jakarta.apache.org/commons/httpclient/redirects.html Oleg -Original Message- From: Sunil Kumar K [mailto:[EMAIL PROTECTED] Sent: Tuesday, May 27, 2003 16:17 To: Commons HttpClient Project Subject: Redirect Issue Guys, Please help me out!. I'm trying to use HttpClient to send HTTP POST Messages.. I'm getting this error... Does it mean that I cannot send message to URL which does not support redirecting.. or I've to set redirect to true... somewhere May 27, 2003 7:40:04 PM org.apache.commons.httpclient.HttpMethodBase processRedirectResponse INFO: Redirect requested but followRedirects is disabled *** Response *** Status Line: HTTP/1.0 302 Found *** Response Body *** cheers! Sunil Assumption is the mother of all screw-ups - To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
Re: Redirect Issue
Thanks Adrian... Will do that... - Original Message - From: Adrian Sutton [EMAIL PROTECTED] To: Commons HttpClient Project [EMAIL PROTECTED] Sent: Wednesday, May 28, 2003 11:33 AM Subject: Re: Redirect Issue | Use the logging features to see exactly what was sent back (and make | sure you're cookies are in compatibility mode). You can find more info | on logging at http://jakarta.apache.org/commons/httpclient/logging.html | | Cookies are generally Oleg's realm but I'd pretty much guarantee he'll | need a wire trace log to track down the problem. :) | | Regards, | | Adrian Sutton. | | On Wednesday, May 28, 2003, at 03:44 PM, Sunil Kumar K wrote: | | Thanks Oleg, | | I tried that, now I'm getting this error.. | | WARNING: Invalid cookie header: DOMS=; PATH=. Missing value for path | attribute | | What should I do...?? | | regards | sunil | | - Original Message - | From: Kalnichevski, Oleg [EMAIL PROTECTED] | To: Commons HttpClient Project | [EMAIL PROTECTED]; | Sunil Kumar Kolar [EMAIL PROTECTED] | Sent: Tuesday, May 27, 2003 8:52 PM | Subject: RE: Redirect Issue | | | Sunil, | | At the moment HttpClient does not support automatic POST redirects. | All you have | to do it is to manually issue GET request to the location specified in | the | redirect response. Have a look at the following resource for more | details | | http://jakarta.apache.org/commons/httpclient/redirects.html | | Oleg | | -Original Message- | From: Sunil Kumar K [mailto:[EMAIL PROTECTED] | Sent: Tuesday, May 27, 2003 16:17 | To: Commons HttpClient Project | Subject: Redirect Issue | | | Guys, | | Please help me out!. | | I'm trying to use HttpClient to send HTTP POST Messages.. | I'm getting this error... | Does it mean that I cannot send message to URL which does not support | redirecting.. | or I've to set redirect to true... somewhere | | May 27, 2003 7:40:04 PM org.apache.commons.httpclient.HttpMethodBase | processRedirectResponse | INFO: Redirect requested but followRedirects is disabled | *** Response *** | Status Line: HTTP/1.0 302 Found | *** Response Body *** | | | cheers! | Sunil | | Assumption is the mother of all screw-ups | | | | - | To unsubscribe, e-mail: [EMAIL PROTECTED] | For additional commands, e-mail: [EMAIL PROTECTED] | - To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
Re: Redirect Issue
) 2003/05/28 11:46:33:914 EDT [TRACE] HeaderElement - -enter HeaderElement.parsePair(char[], int, int) 2003/05/28 11:46:34:055 EDT [TRACE] Cookie - -enter Cookie(String, String, String, String, Date, boolean) 2003/05/28 11:46:34:058 EDT [WARN] HttpMethod - -Invalid cookie header: DOMS=; PATH=. Missing value for path attribute 2003/05/28 11:46:34:058 EDT [TRACE] GetMethod - -enter GetMethod.readResponseBody(HttpState, HttpConnection) 2003/05/28 11:46:34:058 EDT [TRACE] HttpMethod - -enter HttpMethodBase.readResponseBody(HttpState, HttpConnection) 2003/05/28 11:46:34:059 EDT [TRACE] HttpMethod - -enter HttpMethodBase.readResponseBody(HttpState, HttpConnection) 2003/05/28 11:46:34:059 EDT [TRACE] HttpConnection - -enter HttpConnection.getResponseInputStream() 2003/05/28 11:46:34:059 EDT [TRACE] HttpMethod - -enter HttpMethodBase.canResponseHaveBody(int) 2003/05/28 11:46:34:059 EDT [TRACE] HttpMethod - -enter writeRemainingRequestBody(HttpState, HttpConnection) Going out of RUN Time: 13.219 OK (1 tests) - Original Message - From: Adrian Sutton [EMAIL PROTECTED] To: Commons HttpClient Project [EMAIL PROTECTED] Sent: Wednesday, May 28, 2003 11:33 AM Subject: Re: Redirect Issue | Use the logging features to see exactly what was sent back (and make | sure you're cookies are in compatibility mode). You can find more info | on logging at http://jakarta.apache.org/commons/httpclient/logging.html | | Cookies are generally Oleg's realm but I'd pretty much guarantee he'll | need a wire trace log to track down the problem. :) | | Regards, | | Adrian Sutton. | | On Wednesday, May 28, 2003, at 03:44 PM, Sunil Kumar K wrote: | | Thanks Oleg, | | I tried that, now I'm getting this error.. | | WARNING: Invalid cookie header: DOMS=; PATH=. Missing value for path | attribute | | What should I do...?? | | regards | sunil | | - Original Message - | From: Kalnichevski, Oleg [EMAIL PROTECTED] | To: Commons HttpClient Project | [EMAIL PROTECTED]; | Sunil Kumar Kolar [EMAIL PROTECTED] | Sent: Tuesday, May 27, 2003 8:52 PM | Subject: RE: Redirect Issue | | | Sunil, | | At the moment HttpClient does not support automatic POST redirects. | All you have | to do it is to manually issue GET request to the location specified in | the | redirect response. Have a look at the following resource for more | details | | http://jakarta.apache.org/commons/httpclient/redirects.html | | Oleg | | -Original Message- | From: Sunil Kumar K [mailto:[EMAIL PROTECTED] | Sent: Tuesday, May 27, 2003 16:17 | To: Commons HttpClient Project | Subject: Redirect Issue | | | Guys, | | Please help me out!. | | I'm trying to use HttpClient to send HTTP POST Messages.. | I'm getting this error... | Does it mean that I cannot send message to URL which does not support | redirecting.. | or I've to set redirect to true... somewhere | | May 27, 2003 7:40:04 PM org.apache.commons.httpclient.HttpMethodBase | processRedirectResponse | INFO: Redirect requested but followRedirects is disabled | *** Response *** | Status Line: HTTP/1.0 302 Found | *** Response Body *** | | | cheers! | Sunil | | Assumption is the mother of all screw-ups | | | | - | To unsubscribe, e-mail: [EMAIL PROTECTED] | For additional commands, e-mail: [EMAIL PROTECTED] | - To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
Re: Redirect Issue
Sunil, a wirelog would be a lot more interesting. - To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
Re: Redirect Issue
Hi again Sunil, I'm doing too many things at once here so I could be wrong, but would http://nagoya.apache.org/bugzilla/show_bug.cgi?id=20240 be the same problem you're having? If so, you should be able to upgrade to the latest nightly build and the problem would be fixed. I can't be sure if the fix made it into beta 1 or not - it's definitely worth a look anyway. Otherwise I promise to shutup and let Oleg impart his wisdom. :) Adrian. On Wednesday, May 28, 2003, at 04:33 PM, Sunil Kumar K wrote: 2003/05/28 11:46:33:901 EDT [TRACE] HttpParser - -enter HttpConnection.readRawLine() 2003/05/28 11:46:33:902 EDT [DEBUG] wire - - Set-Cookie: DOMS=; PATH= [\r\n] - To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
RE: Redirect Issue
Sunil, 'Set-Cookie: DOMS=; PATH=' cookie is rejected as it violates the RFC2109 specification. Here's what you should do to work the problem around: 1) Upgrade to the latest nightly build (commons-httpclient-20030527.tar.gz) http://cvs.apache.org/builds/jakarta-commons/nightly/commons-httpclient/ 2) Use compatibility cookie policy. See examples below http://cvs.apache.org/viewcvs/jakarta-commons/httpclient/src/examples/CookieDemoApp.java?rev=HEADcontent-type=text/vnd.viewcvs-markup That should help Cheers Oleg - To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
Re: Redirect Issue
Hi, I think I should explain you the problem at hand clearly. Users come to our site A and provide some information on page N. Our application should process the information and depending on the user should send POST data to other URLs. Those URLs are supposed to receive dat and display their page back. So users are being redirected to diff site page. I'm using httpclient to send POST message. Should I need to get the response back and display it OR sending POST message is enough and browser will get new page from site B. Thanks in advance! regards Sunil - Original Message - From: Kalnichevski, Oleg [EMAIL PROTECTED] To: Commons HttpClient Project [EMAIL PROTECTED]; Sunil Kumar Kolar [EMAIL PROTECTED] Sent: Wednesday, May 28, 2003 12:52 PM Subject: RE: Redirect Issue Sunil, 'Set-Cookie: DOMS=; PATH=' cookie is rejected as it violates the RFC2109 specification. Here's what you should do to work the problem around: 1) Upgrade to the latest nightly build (commons-httpclient-20030527.tar.gz) http://cvs.apache.org/builds/jakarta-commons/nightly/commons-httpclient/ 2) Use compatibility cookie policy. See examples below http://cvs.apache.org/viewcvs/jakarta-commons/httpclient/src/examples/CookieDemo App.java?rev=HEADcontent-type=text/vnd.viewcvs-markup That should help Cheers Oleg - To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] - To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
Re: Redirect Issue
Sunil Kumar K wrote: Should I need to get the response back and display it OR sending POST message is enough and browser will get new page from site B. Sunil, The user's browser only gets what you write to the ServletOutputStream of your servlet. This is not an issue with HttpClient. Please refer to text books on Servlet Programming. Odi - To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
Re: Redirect Issue
Ya I relealised that But I also had some HTTPClient issues. Oleg and Adrian helped to solve those issues... Thanks guys... - Original Message - From: Ortwin Glück [EMAIL PROTECTED] To: Commons HttpClient Project [EMAIL PROTECTED] Sent: Wednesday, May 28, 2003 3:07 PM Subject: Re: Redirect Issue | Sunil Kumar K wrote: | Should I | need to get the response back and display it OR sending POST message | is enough and browser will get new page from site B. | | Sunil, | | The user's browser only gets what you write to the ServletOutputStream | of your servlet. This is not an issue with HttpClient. Please refer to | text books on Servlet Programming. | | Odi | | | - | To unsubscribe, e-mail: [EMAIL PROTECTED] | For additional commands, e-mail: [EMAIL PROTECTED] | - To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
Re: Redirect Issue
Thanks Oleg, I tried that, now I'm getting this error.. WARNING: Invalid cookie header: DOMS=; PATH=. Missing value for path attribute What should I do...?? regards sunil - Original Message - From: Kalnichevski, Oleg [EMAIL PROTECTED] To: Commons HttpClient Project [EMAIL PROTECTED]; Sunil Kumar Kolar [EMAIL PROTECTED] Sent: Tuesday, May 27, 2003 8:52 PM Subject: RE: Redirect Issue Sunil, At the moment HttpClient does not support automatic POST redirects. All you have to do it is to manually issue GET request to the location specified in the redirect response. Have a look at the following resource for more details http://jakarta.apache.org/commons/httpclient/redirects.html Oleg -Original Message- From: Sunil Kumar K [mailto:[EMAIL PROTECTED] Sent: Tuesday, May 27, 2003 16:17 To: Commons HttpClient Project Subject: Redirect Issue Guys, Please help me out!. I'm trying to use HttpClient to send HTTP POST Messages.. I'm getting this error... Does it mean that I cannot send message to URL which does not support redirecting.. or I've to set redirect to true... somewhere May 27, 2003 7:40:04 PM org.apache.commons.httpclient.HttpMethodBase processRedirectResponse INFO: Redirect requested but followRedirects is disabled *** Response *** Status Line: HTTP/1.0 302 Found *** Response Body *** cheers! Sunil Assumption is the mother of all screw-ups - To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] - To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] - To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]