Httpclient4.3实例。 每个版本接口变更都巨大

 

1.新增简单的url请求内容返回, 比较时髦的链调用

 

		try {
			Content returnContent = Request.Get("http://www.qq.com")
			.execute().returnContent();
			System.out.println(returnContent.toString());
		} catch (ClientProtocolException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

 

2. 一套demo方法, 含模拟登录,获取cookie,并把cookie带入后面的请求中

 

		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();
			}
		}	

 

 HttpGet 参数拼装代码示例:

		String base = "http://api.map.baidu.com/geosearch/v3/nearby?";

	    List<NameValuePair> params = new LinkedList<NameValuePair>();
		params.add(new BasicNameValuePair("ak", JobConstans.ak));
		params.add(new BasicNameValuePair("geotable_id", geotable_id));
		params.add(new BasicNameValuePair("filter", filter.filter));
		params.add(new BasicNameValuePair("page_index", String.valueOf(filter.page_index)));
		params.add(new BasicNameValuePair("page_size", String.valueOf(filter.page_size)));
		params.add(new BasicNameValuePair("location", filter.location));
		params.add(new BasicNameValuePair("sortby", filter.sortby));
		params.add(new BasicNameValuePair("radius", String.valueOf(filter.radius)));
	    String paramString = URLEncodedUtils.format(params, "utf-8");
	    base += paramString;

 

你可能感兴趣的:(httpclient)