在Java中处理重定向302
发布时间:2020-12-15 02:30:13 所属栏目:Java 来源:网络整理
导读:我一直在尝试处理 java代码中的重定向(302)……以及我使用的代码 org.apache.commons.httpclient.HttpClient没有声明setRedirectStrategy(),所以我必须编写自己的重定向实现: private void loadHttp302Request(HttpMethod method,HttpClient client,int sta
我一直在尝试处理
java代码中的重定向(302)……以及我使用的代码
org.apache.commons.httpclient.HttpClient没有声明setRedirectStrategy(),所以我必须编写自己的重定向实现: private void loadHttp302Request(HttpMethod method,HttpClient client,int status,String urlString) throws HttpException,IOException { if (status!=302) return; String[] url = urlString.split("/"); logger.debug("Method -> loadHttp302Request -> Location : " + method.getResponseHeader("Location") .getValue()); logger.debug("Method -> loadHttp302Request -> Cookies : " + method.getResponseHeader("Set-Cookie") .getValue()); logger.debug("Method -> loadHttp302Request -> Referrer : " + url[0]+"//"+url[2]); HttpMethod theMethod = new GetMethod(urlString+method.getResponseHeader("Location") .getValue()); theMethod.setRequestHeader("Cookie",method.getResponseHeader("Set-Cookie") .getValue()); theMethod.setRequestHeader("Referrer",url[0]+"//"+url[2]); int _status = client.executeMethod(theMethod); logger.debug("Method -> loadHttp302Request -> Status : " + _status); method = theMethod; } 执行此操作后,状态代码等于200,因此看起来一切正常,但响应主体和响应流都为空.我已经能够使用wireshark来嗅探TCP流,就Wireshark而言,我从我的重定向代码中收回了整个响应主体……所以我不确定我做错了什么或者下一步要找什么…理想情况如果我可以使用setRedirectStrategy()会很好,但因为它是客户端的代码:p我使用org.apache.commons.httpclient.HttpClient卡住… 我调试了executeMethod()并发现当从输入流中读取响应时,它似乎什么也得不到,即使wireshark肯定表明我收到了完整的响应体. 任何想法,将不胜感激 :) 解决方法
method = theMethod;在loadHttp302Request结束时不会做任何有用的事情.当loadHttp302Request返回时,调用(java)方法中的方法对象指针仍将指向原始的HttpMethod对象.
从loadHttp302Request返回theMethod并从中获取响应内容. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |