inputurl="http://***.com/common/getEmployeeByDept.do?dept1=D032&dept2=D032002&dept3=D032010"
//创建HttpClientBuilder
HttpClientBuilder httpClientBuilder = HttpClientBuilder.create();
//HttpClient
CloseableHttpClient httpClient = httpClientBuilder.build();
HttpGet httpGet = new HttpGet(inputurl);
返回信息如下:{"code":"400","data":null,"msg":"请重新登录。"}
HttpHost httpHost = new HttpHost("localhost");
HttpGet httpGet = new HttpGet("/https/");
HttpResponse response = httpClient.execute(httpHost,httpGet);
if(HttpStatus.SC_OK==response.getStatusLine().getStatusCode()){
HttpEntity entity = response.getEntity();
}
httpGet = new HttpGet("/https/index.jsp?cookie=write");
response = httpClient.execute(httpHost,httpGet);
FileWriter fw = new FileWriter("C:/cookie.txt");
//读取cookie并保存文件
List cookies = ((AbstractHttpClient) httpClient).getCookieStore().getCookies();
<dependency>
<groupId>net.sourceforge.htmlunit</groupId>
<artifactId>htmlunit</artifactId>
<version>2.15</version>
</dependency>
2.使用Htmlunit提取cookie:final WebClient webClient = new WebClient(BrowserVersion.CHROME);
webClient.getOptions().setUseInsecureSSL(true);
webClient.getOptions().setJavaScriptEnabled(false);
webClient.getOptions().setCssEnabled(false);
HtmlPage page = null;
page = webClient.getPage(loginUrl);
HtmlElement corpid = (HtmlElement) page.getElementById("id_corpid");
HtmlElement corppw = (HtmlElement) page.getElementById("id_corppw");
corpid.click();
String username = ph.readValue("userName");
corpid.type(username);
corppw.click();
String password = new String(dec.decodeBuffer(ph.readValue("password")));
corppw.type(password);
List loginBtn = (List) page.getByXPath("//div[@id=‘corp‘]/form/div[@]/button");
Page resultPage = loginBtn.get(0).click();
String EHRCookie = HtmlUnitUtil.getCookieHeader(webClient);
logger.info("获得openidpage cookie值: "+cookie);
return cookie;
执行会发现已经将cookie提取出来:[INFO ]13:29:15,882,main,[Class]LoginUtil, [Method]getEHRCookie, 获得openidpage cookie值: JSESSIONID=8A1979AC21C24DD8622E41D89ABFF6F3.classa-***.org-8010; sessionid=23d2b3e737c34cb8d8898bbec94c6a11
3.接着在Httpclient中加载提取的cookie访问http接口://加载cookie
httpGet.addHeader(new BasicHeader("Cookie",cookie));
原文:http://www.cnblogs.com/appstest/p/4962336.html