HttpClient 4.5.3( get和post请求)
Posted heqiyoujing
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了HttpClient 4.5.3( get和post请求)相关的知识,希望对你有一定的参考价值。
版本HttpCilent 4.5.3
1.GET请求
CloseableHttpClient httpCilent = HttpClients.createDefault();//Creates CloseableHttpClient instance with default configuration. HttpGet httpGet = new HttpGet("http://www.baidu.com"); try { httpCilent.execute(httpGet); } catch (IOException e) { e.printStackTrace(); }finally { try { httpCilent.close();//释放资源 } catch (IOException e) { e.printStackTrace(); } }
2.GET请求,设置 超时时间
CloseableHttpClient httpCilent2 = HttpClients.createDefault(); RequestConfig requestConfig = RequestConfig.custom() .setConnectTimeout(5000) //设置连接超时时间 .setConnectionRequestTimeout(5000) // 设置请求超时时间 .setSocketTimeout(5000) .setRedirectsEnabled(true)//默认允许自动重定向 .build(); HttpGet httpGet2 = new HttpGet("http://www.baidu.com"); httpGet2.setConfig(requestConfig); String srtResult = ""; try { HttpResponse httpResponse = httpCilent2.execute(httpGet2); if(httpResponse.getStatusLine().getStatusCode() == 200){ srtResult = EntityUtils.toString(httpResponse.getEntity());//获得返回的结果 System.out.println(srtResult); }else if(httpResponse.getStatusLine().getStatusCode() == 400){ //.......... }else if(httpResponse.getStatusLine().getStatusCode() == 500){ //............. } } catch (IOException e) { e.printStackTrace(); }finally { try { httpCilent2.close(); } catch (IOException e) { e.printStackTrace(); } }
3.POST请求,参数
//获取可关闭的 httpCilent CloseableHttpClient httpClient = HttpClients.createDefault(); //配置超时时间 RequestConfig requestConfig = RequestConfig.custom(). setConnectTimeout(1000).setConnectionRequestTimeout(1000) .setSocketTimeout(1000).setRedirectsEnabled(true).build(); HttpPost httpPost = new HttpPost("http://consentprt.dtac.co.th/webaoc/123SubscriberProcess"); //设置超时时间 httpPost.setConfig(requestConfig); //装配post请求参数 List<BasicNameValuePair> list = new ArrayList<BasicNameValuePair>(); list.add(new BasicNameValuePair("age", "20")); //请求参数 list.add(new BasicNameValuePair("name", "zhangsan")); //请求参数 try { UrlEncodedFormEntity entity = new UrlEncodedFormEntity(list,"UTF-8"); //设置post求情参数 httpPost.setEntity(entity); HttpResponse httpResponse = httpClient.execute(httpPost); String strResult = ""; if(httpResponse != null){ System.out.println(httpResponse.getStatusLine().getStatusCode()); if (httpResponse.getStatusLine().getStatusCode() == 200) { strResult = EntityUtils.toString(httpResponse.getEntity()); } else if (httpResponse.getStatusLine().getStatusCode() == 400) { strResult = "Error Response: " + response.getStatusLine().toString(); } else if (httpResponse.getStatusLine().getStatusCode() == 500) { strResult = "Error Response: " + response.getStatusLine().toString(); } else { strResult = "Error Response: " + response.getStatusLine().toString(); } }else{ } System.out.println(strResult); } catch (Exception e) { e.printStackTrace(); }finally { try { if(httpClient != null){ httpClient.close(); //释放资源 } } catch (IOException e) { e.printStackTrace(); } }
4.Post 请求,map参数
public static String doPost(String url, Map<String, Object> paramsMap){ CloseableHttpClient httpClient = HttpClients.createDefault(); HttpPost httpPost = new HttpPost(url); RequestConfig requestConfig = RequestConfig.custom(). setConnectTimeout(180 * 1000).setConnectionRequestTimeout(180 * 1000) .setSocketTimeout(180 * 1000).setRedirectsEnabled(true).build(); httpPost.setConfig(requestConfig); List<NameValuePair> nvps = new ArrayList<NameValuePair>(); for (String key : paramsMap.keySet()) { nvps.add(new BasicNameValuePair(key, String.valueOf(paramsMap.get(key)))); } try { httpPost.setEntity(new UrlEncodedFormEntity(nvps, "UTF-8")); logger.info("httpPost ===**********===>>> " + EntityUtils.toString(httpPost.getEntity())); HttpResponse response = httpClient.execute(httpPost); String strResult = ""; if (response.getStatusLine().getStatusCode() == 200) { strResult = EntityUtils.toString(response.getEntity()); return strResult; } else { return "Error Response: " + response.getStatusLine().toString(); } } catch (Exception e) { e.printStackTrace(); return "post failure :caused by-->" + e.getMessage().toString(); }finally { if(null != httpClient){ try { httpClient.close(); } catch (IOException e) { e.printStackTrace(); } } } }
5.POST 请求,参数是json字符串
public static String doPostForJson(String url, String jsonParams){ CloseableHttpClient httpClient = HttpClients.createDefault(); HttpPost httpPost = new HttpPost(url); RequestConfig requestConfig = RequestConfig.custom(). setConnectTimeout(180 * 1000).setConnectionRequestTimeout(180 * 1000) .setSocketTimeout(180 * 1000).setRedirectsEnabled(true).build(); httpPost.setConfig(requestConfig); httpPost.setHeader("Content-Type","application/json"); // try { httpPost.setEntity(new StringEntity(jsonParams,ContentType.create("application/json", "utf-8"))); System.out.println("request parameters" + EntityUtils.toString(httpPost.getEntity())); HttpResponse response = httpClient.execute(httpPost); System.out.println(" code:"+response.getStatusLine().getStatusCode()); System.out.println("doPostForInfobipUnsub response"+response.getStatusLine().toString()); return String.valueOf(response.getStatusLine().getStatusCode()); } catch (Exception e) { e.printStackTrace(); return "post failure :caused by-->" + e.getMessage().toString(); }finally { if(null != httpClient){ try { httpClient.close(); } catch (IOException e) { e.printStackTrace(); } } } }
以上是关于HttpClient 4.5.3( get和post请求)的主要内容,如果未能解决你的问题,请参考以下文章
启动 OSGi 模块 httpclient-osgi-4.5.3.jar 时出现 Liferay SAXException
使用Apache HttpClient 4.x发送Json数据