如何使用 HttpClient 获取 Cookie
您好,我正在使用 HttpClient 获取 Cookie,但我找不到任何 Cookie。我的代码如下:
public class LoginTab {
private Cookie[] cookies;
HttpClient httpClient;
HttpState httpState;
HashMap postData;
public LoginTab() {
httpClient = new HttpClient();
httpState = new HttpState();
httpClient.getHttpConnectionManager().
getParams().setConnectionTimeout(300000);
httpClient.setState(httpState);
// RFC 2101 cookie management spec is used per default
// to parse, validate, format & match cookies
httpClient.getParams().setCookiePolicy(CookiePolicy.RFC_2109);
postData= new HashMap();
}
public String getMethod(String url) {
GetMethod getMethod = new GetMethod(url);
String pageSoure="";
try{
httpClient.executeMethod(getMethod);
pageSoure=getMethod.getResponseBodyAsString();
extractUsefulPostData(pageSoure, postData);
getMethod.releaseConnection();
}catch(Exception ex)
{
ex.printStackTrace();
}
return pageSoure;
}
public static void main(String[]arg)
{
LoginTab loginTab= new LoginTab();
System.out.println(loginTab.getMethod("http://tab.com.au/"));
Cookie [] cookies=loginTab.httpState.getCookies();
System.out.println(cookies.length);
for(int i=0;i<cookies.length;i++)
System.out.println(cookies[i]);
}
}
请告诉我我犯了一个错误。 提前致谢
Hello I am using HttpClient to get Cookies but I am unable find any cookies. My Code is given below:
public class LoginTab {
private Cookie[] cookies;
HttpClient httpClient;
HttpState httpState;
HashMap postData;
public LoginTab() {
httpClient = new HttpClient();
httpState = new HttpState();
httpClient.getHttpConnectionManager().
getParams().setConnectionTimeout(300000);
httpClient.setState(httpState);
// RFC 2101 cookie management spec is used per default
// to parse, validate, format & match cookies
httpClient.getParams().setCookiePolicy(CookiePolicy.RFC_2109);
postData= new HashMap();
}
public String getMethod(String url) {
GetMethod getMethod = new GetMethod(url);
String pageSoure="";
try{
httpClient.executeMethod(getMethod);
pageSoure=getMethod.getResponseBodyAsString();
extractUsefulPostData(pageSoure, postData);
getMethod.releaseConnection();
}catch(Exception ex)
{
ex.printStackTrace();
}
return pageSoure;
}
public static void main(String[]arg)
{
LoginTab loginTab= new LoginTab();
System.out.println(loginTab.getMethod("http://tab.com.au/"));
Cookie [] cookies=loginTab.httpState.getCookies();
System.out.println(cookies.length);
for(int i=0;i<cookies.length;i++)
System.out.println(cookies[i]);
}
}
Please tell me I've made a mistake.
Thanks in advance
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我尝试了所有可用的 CookiePolicy 并设置了“用户代理”内容,但没有成功。
从我对 java - verbose 的观察来看, getCookie() 方法中存在一个捕获异常,该异常会引发:
可能从站点收到的 cookie (我可以使用 Firefox 上的 Cookie 编辑器看到它)有一些对 httpclient 不利的东西;查看 此 线程同样的问题。
据我所知,您需要启用日志记录并检查 httpclient 对 cookie 解析的作用。
I have tried with all the CookiePolicy available and also setting "user-agent" stuff without success.
From what i see with java - verbose there's a trapped exception in getCookie() method that raises:
Probably cookie received from site (i can see it with Cookie Editor on firefox) has something not good for httpclient; have a look at this thread for the same problem.
From what i see, you need to enable logging and check what httpclient does on cookie parsing.