Httpclient4.3实例。 每个版本接口变更都巨大
CookieStore cookieStore = new BasicCookieStore();HttpClientContext context = HttpClientContext.create();context.setCookieStore(cookieStore);RequestConfig globalConfig = RequestConfig.custom().setCookieSpec(CookieSpecs.BROWSER_COMPATIBILITY).build();CloseableHttpClient httpclient = HttpClients.custom().setDefaultRequestConfig(globalConfig).setDefaultCookieStore(cookieStore).build();// 登录HttpPost loginPost = new HttpPost("https://www.xxx.com/account/login.php");List<NameValuePair> nvps = new ArrayList<NameValuePair>(); nvps.add(new BasicNameValuePair("email", email)); nvps.add(new BasicNameValuePair("password", pwd)); loginPost.setEntity(new UrlEncodedFormEntity(nvps,"UTF-8")); CloseableHttpResponse loginResp = httpclient.execute(loginPost,context); try {// System.out.println(loginResp.getStatusLine()); HttpEntity entity1 = loginResp.getEntity(); EntityUtils.consume(entity1); } finally { loginResp.close(); } for (int i = START_PAGE; i < END_PAGE; i++) { Thread.sleep(SLEEP_TIME);//获取交易HttpGet get = new HttpGet("https://www.xxx.com/trade/index.php?a=history&t=0&amt_begin=0&amt_end=0&date_begin=&date_end=&pn="+i);HttpClientContext context1 = HttpClientContext.create();context1.setCookieStore(cookieStore);HttpResponse dealResp = httpclient.execute(get, context1);try {HttpEntity dealEntity = dealResp.getEntity();String body = EntityUtils.toString(dealEntity);prasepage(body);EntityUtils.consume(dealEntity);} finally {loginResp.close();}}
?
?