zoukankan      html  css  js  c++  java
  • java使用HttpClient

    HttpClient常用的包有两个

    org.apache.http.client以及org.apache.commons.httpclient

    我常用的是org.apache.http.client。

    HttpClient在4.3版本以后声明HttpClient的方法和以前略有区别,不再是直接声明new DefaultHttpClient() .

    参考下文:

    new DefaultHttpClient过时处理建议和HTTP调用后关闭流处理

    HttpClient 4.5.2版本设置连接超时时间-CloseableHttpClient设置Timeout

    JAVA发送HttpClient请求及接收请求结果过程

    HttpClient使用详解

     

    代码如下:

     

        public static void sendHttpAudit(OAApplicationModule am, String type, 
                                       String entityMappingId, String param) {
            System.out.println(System.currentTimeMillis());
    
            String oaRestfulServiceUrl = null;
    //        am.getOADBTransaction().getProfile("");
              oaRestfulServiceUrl =  "http://xxx.xxx.xxx.xxx:8090/api/Service/submitXXX";
              
    //        RequestConfig requestConfig = 
    //            RequestConfig.custom().setConnectTimeout(5000).setConnectionRequestTimeout(1000).setSocketTimeout(5000).build();
            RequestConfig requestConfig = 
                RequestConfig.custom().setConnectTimeout(5000).setConnectionRequestTimeout(1000).build();   
            CloseableHttpClient httpclient = null;
             
            CloseableHttpResponse h_response = null;
            
            try {
                httpclient =   HttpClientBuilder.create().setDefaultRequestConfig(requestConfig).build();
    //            httpclient = HttpClientBuilder.create().build();
                
                HttpPost postMethod = new HttpPost(oaRestfulServiceUrl);
    //            postMethod.setConfig(requestConfig);
                postMethod.setHeader("User-Agent", 
                                     "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/40.0.2214.111 Safari/537.36");
                postMethod.setHeader("Referer", oaRestfulServiceUrl);
                List<NameValuePair> params = new ArrayList<NameValuePair>();
                //200_ifaceCode 200表示为EBS系统调用RESTFUL服务。
                params.add(new BasicNameValuePair("type",  type));            
                params.add(new BasicNameValuePair("param", param));
                //添加参数
                postMethod.setEntity(new UrlEncodedFormEntity(params, "UTF-8"));
                h_response = httpclient.execute(postMethod);
                HttpEntity repEntity = h_response.getEntity();
    
                int statusCode = h_response.getStatusLine().getStatusCode();
                if (statusCode != HttpStatus.SC_OK) {
                    postMethod.abort();
                    procProcessError(am, entityMappingId, String.valueOf(statusCode), EntityUtils.toString(repEntity, "UTF-8") );
                    throw new OAException("HttpClient,error status code :" + 
                                          statusCode);
                }
                String content = EntityUtils.toString(repEntity, "UTF-8");
                System.out.println(content);
                JSONObject json = JSON.parseObject(content);
                String retCode = json.getString("returnCode");
                String retMsg   = json.getString("returnMsg");
                procProcessError(am, entityMappingId, retCode, retMsg );
                if (!"000000".equals(retCode)) {
                    System.out.println("  error  " +retMsg );
                    throw new OAException("接口服务处理异常" + 
                                          json.getString("returnMsg"));
                } else {
                    changeStatusInprocess(am, entityMappingId);
                }
            } catch (ConnectTimeoutException e) {
                procProcessError(am, entityMappingId, "ConnectTimeoutException", e.getMessage() );
            
                System.out.println(" ConnectTimeoutException " + 
                                   System.currentTimeMillis());
                e.printStackTrace();
                throw new OAException("接口服务处理异常 ConnectTimeoutException " + 
                                      e.getMessage());
            } catch (SocketTimeoutException e) {
                procProcessError(am, entityMappingId, "SocketTimeoutException", e.getMessage() );
                System.out.println(" SocketTimeoutException " + 
                                   System.currentTimeMillis());
                e.printStackTrace();
                throw new OAException("接口服务处理异常 SocketTimeoutException " + 
                                      e.getMessage());
            } catch (Exception e) {
                procProcessError(am, entityMappingId, "Exception", e.getMessage() );
                System.out.println("Exception " + System.currentTimeMillis());
                e.printStackTrace();
                throw new OAException("接口服务处理异常 Exception " + e.getMessage());
            } finally {
                if (h_response != null) {
                    try {
                        h_response.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
                if (httpclient != null) {
                    try {
                        httpclient.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
        }

     

     

     

    org.apache.commons.httpclient参考如下

    httpClient 超时时间设置(转)

  • 相关阅读:
    SED{shell三剑客awk,sed,grep}
    AWK{shell三剑客awk,sed,grep}
    面试汇总
    shell脚本
    redis主从
    haproxy
    grafana
    zabbix
    lnmp
    shell 基础
  • 原文地址:https://www.cnblogs.com/huanghongbo/p/7843507.html
Copyright © 2011-2022 走看看