HttpClient 4.5.3 get和post请求
HttpCilent 4.5.3
GET请求
1 2 3 4 5 6 7 8 9 10 11 12 13 | 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(); } } |
GET 设置 超时时间
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 | 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(); } } |
POST请求
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 | //获取可关闭的 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(); } } |
Post 请求
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 | 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(); } } } } |
POST 请求,参数是json字符串
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 | 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(); } } } } |