java HttpClient模拟登陆知乎问答网站,获取不到cookie

「已注销」 2012-11-04 08:05:14
java HttpClient模拟登陆知乎问答网站,获取不到cookie
求解??????
form表单action 地址是https://www.zhihu.com
代码如下:

public static void main(String[] args){
String loginUrl = "http://www.zhihu.com";
String username = "XXXXXXXXXXXX";
String password = "XXXXXXXXXXXX";
String htmlGetSource = "";
HttpClient httpClient = new DefaultHttpClient();

httpClient.getParams().setParameter(CoreProtocolPNames.USER_AGENT, "Mozilla/5.0 (X11; U; Linux i686; zh-CN; rv:1.9.1.2) Gecko/20090803 Fedora/3.5.2-2.fc11 Firefox/3.5.2");
//获取登录框的隐含参数 type="hidden" name="_xsrf"
try {
HttpGet httpGet = new HttpGet(loginUrl);
HttpResponse response = httpClient.execute(httpGet);
HttpEntity entity = response.getEntity();
if(entity!=null){
InputStream in = entity.getContent();
String str = "";
BufferedReader br = new BufferedReader(new InputStreamReader(in,"UTF-8"));
while((str=br.readLine())!=null){
htmlGetSource += str+"\n";
}
}
} catch (ClientProtocolException e1) {
e1.printStackTrace();
} catch (IOException e1) {
e1.printStackTrace();
}
//应用JsoupHtml解析包解析html包含参数
Document doc = Jsoup.parse(htmlGetSource);
Element formContent = null;
Element inputContent = null;
String actionStr = "";
String _xsrf="";
String checked = "on";
formContent = doc.select("form[method=post]").first();
inputContent = doc.select("input[type=hidden]").first();
actionStr = formContent.attr("action");
_xsrf = inputContent.attr("value");
System.out.println(actionStr);

HttpPost httpPost = new HttpPost(actionStr);
HttpResponse response;
//请求Header
httpPost.setHeader("(Request-Line)","POST /login HTTP/1.1");
httpPost.setHeader("User-Agent","Mozilla/5.0 (Windows NT 6.1; WOW64; rv:15.0) Gecko/20100101 Firefox/15.0.1");
httpPost.setHeader("Referer", "http://www.zhihu.com/");
httpPost.setHeader("Content-Type", "application/x-www-form-urlencoded");
httpPost.setHeader("Accept-Language","zh-cn,zh;q=0.8,en-us;q=0.5,en;q=0.3");
httpPost.setHeader("Accept-Encoding","gzip, deflate");
httpPost.setHeader("Accept","text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");
httpPost.setHeader("Connection","keep-alive");
//POST参数
List<NameValuePair> nvps = new ArrayList<NameValuePair>();
nvps.add(new BasicNameValuePair("_xsrf", _xsrf));
nvps.add(new BasicNameValuePair("email", username));
nvps.add(new BasicNameValuePair("password", password));
nvps.add(new BasicNameValuePair("rememberme", "on"));

try {
httpPost.setEntity(new UrlEncodedFormEntity(nvps,HTTP.UTF_8));
response = httpClient.execute(httpPost);
//Header[] headers = response.getAllHeaders();
//for(Header h:headers){
// System.out.println(h);
//}
//得到cookie
String set_cookie = response.getFirstHeader("Set-Cookie").getValue();
System.out.println(set_cookie);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
httpPost.abort();
}
}
...全文
1231 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
huangzhuangpeng01 2015-01-27
  • 打赏
  • 举报
回复
楼主,你这段代码现在还能不能运行呢?需要导入什么包?为什么我运行到        response = httpClient.execute(httpPost); 这里就出错了。。
qq_24961497 2014-12-31
  • 打赏
  • 举报
回复
response.getFirstHeader("Set-Cookie").getValue(); 是不是这个是空的啊? 我也是这种状况,如果你解决了,告诉我哦
深山老叔 2012-12-27
  • 打赏
  • 举报
回复
好吧,围观下...有些网站会禁用掉这种访问
pws22 2012-12-26
  • 打赏
  • 举报
回复
我怎么post后的状态码是 403
「已注销」 2012-11-04
  • 打赏
  • 举报
回复
次奥……自己把问题解决了,程序也没问题 是输入的用户名错了 路过的围观下就是啦

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧