第一次调用从server获取Cookie
Posted Blade blog
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了第一次调用从server获取Cookie相关的知识,希望对你有一定的参考价值。
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;
}
以上是关于第一次调用从server获取Cookie的主要内容,如果未能解决你的问题,请参考以下文章
从浏览器中删除应用程序 cookie 后调用重定向失败,提示 cors 错误
JAVAWEB开发之Session的追踪创建和销毁JSP具体解释(指令,标签,内置对象,动作即转发和包括)JavaBean及内省技术以及EL表达式获取内容的使用