您的位置:首页 > 其它

第一次调用从server获取Cookie

2017-02-03 17:36 585 查看
System.setProperty("javax.net.ssl.trustStore", certPath);

public String getCookieString(String userId, String pwd) throws Exception {

HttpClient httpclient = HttpClientBuilder.create().build();

HttpPost httpPost = new HttpPost(LONGIN_URL);

httpPost.setHeader("Content-Type", "application/x-www-form-urlencoded");
httpPost.setEntity(getParam(userId, pwd));

HttpResponse response = httpclient.execute(httpPost);

return _getCookieString(response.getAllHeaders());

}

private String _getCookieString(Header[] h) {

String cookieFromServer = "";

for (Header ibh : h) {
System.out.print(ibh.getName());
if (ibh.getName().equals("Set-Cookie")) {
cookieFromServer = cookieFromServer + ibh.getValue();
}
}

return cookieFromServer;
}

private StringEntity getParam(String userName, String pwd) {

String aa = "username=" + userName + "&password=" + pwd + "&requestedHash=";

StringEntity requestEntity = new StringEntity(aa, ContentType.APPLICATION_FORM_URLENCODED);
return requestEntity;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: