HttpClient 4.5.3 get和post请求

原文来自:https://www.cnblogs.com/c9999/p/6636415.html

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

  


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
HttpClient是Java中的一个开源库,用于支持HTTP协议的客户端编程。它是一个用于发送HTTP请求和接收HTTP响应的包装工具类。HttpClient可以被用于执行GET和POST请求等HTTP方法。走看看是一个基于Web的应用程序,包含了各种常见的网站功能,如搜索、资讯、体育、财经、购物等多个频道。下面将分别介绍HttpClient发送GET和POST请求时的一些重要知识点。 HttpClient发送GET请求时,需要构造一个HttpGet对象,并指定请求的URL。调用HttpClient.execute方法,并且将HttpGet对象传递给该方法。接下来,HttpClient会发送GET请求到指定的URL,然后将响应内容作为一个HttpResponse对象返回给程序。可以从HttpResponse对象中获取响应状态、响应头和响应体等信息。 HttpClient发送POST请求时,需要首先构造一个HttpPost对象,并指定请求的URL。调用HttpPost.setEntity方法来设置请求体内容,然后调用HttpClient.execute方法,并将HttpPost对象传递给该方法。接下来,HttpClient会将POST请求数据发送到指定的URL,然后将响应内容作为一个HttpResponse对象返回给程序。与GET请求相似,可以从HttpResponse对象中获取响应状态、响应头和响应体等信息。 总的来说,HttpClient是一个十分强大和方便的网络编程工具类,可以方便地实现HTTP请求和响应的处理。可以根据自己的需求选择GET和POST请求发送,然后获取响应内容和各种信息。使用HttpClient能够简化开发,提高编程效率,是Java网络编程开发中非常重要的一种库。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值