zoukankan      html  css  js  c++  java
  • HttpClient 4.4 请求

    4.2 版本

     1     /**
     2      * url 请求 paramUrl
     3      * 
     4      * @time 2015年11月10日下午4:40:22
     5      * @packageName com.rom.utils
     6      * @param url         url请求地址
     7      * @param header    请求头信息
     8      * @param params     url请求参数
     9      * @param paramstype 参数类型 1:json格式 ; 其他:正常格式;
    10      * @param resulttype 返回值类型 1: 压缩流字符串 2:正常字符串
    11      * @return
    12      */
    13     public synchronized static String paramUrl(String url, Map<String, String> header, Map<String, Object> params,
    14             String paramstype, String resulttype) {
    15 
    16         String result = "";
    17         HttpClient httpClient = new DefaultHttpClient();
    18         httpClient.getParams().setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, 10000);
    19 
    20         HttpPost httpPost = new HttpPost(url);
    21 
    22         if (header != null) {
    23             Set<String> headerkey = header.keySet();
    24             Iterator<String> headerkeyit = headerkey.iterator();
    25             while (headerkeyit.hasNext()) {
    26                 String key = (String) headerkeyit.next();
    27                 httpPost.setHeader(key, header.get(key).toString());
    28             }
    29         }
    30 
    31         try {
    32             // 如果参数类型为1 证明参数传递方式为 json格式
    33             if (paramstype != null && paramstype.equals("1")) {
    34                 if (params != null) {
    35                     StringEntity entity = new StringEntity(JSONObject.fromObject(params).toString(), "utf-8");// 解决中文乱码问题
    36                     entity.setContentEncoding("UTF-8");
    37                     entity.setContentType("application/json");
    38                     httpPost.setEntity(entity);
    39                 }
    40             } else {
    41                 // 参数格式为 键值对形式
    42                 if (params != null) {
    43                     List<NameValuePair> formparams = new ArrayList<NameValuePair>();
    44                     Set<String> keySet = params.keySet();
    45                     Iterator<String> keyit = keySet.iterator();
    46                     while (keyit.hasNext()) {
    47                         String key = (String) keyit.next();
    48                         formparams.add(new BasicNameValuePair(key, params.get(key).toString()));
    49                     }
    50                     UrlEncodedFormEntity uefEntity = new UrlEncodedFormEntity(formparams, "UTF-8");
    51                     httpPost.setEntity(uefEntity);
    52                 }
    53             }
    54 
    55             HttpResponse response = httpClient.execute(httpPost);
    56             HttpEntity entity = response.getEntity();
    57             if (entity != null) {
    58                 // 返回类型如果不为空 并且 等于1 证明该返回结果经过zip压缩
    59                 if (resulttype != null && resulttype.equals("1")) {
    60                     result = EntityUtils.toString(new GzipDecompressingEntity(entity), "utf-8");
    61                 } else {
    62                     result = EntityUtils.toString(entity, "utf-8");
    63                 }
    64             }
    65         } catch (UnsupportedEncodingException e) {
    66             e.printStackTrace();
    67             log.error(e.getMessage());
    68         } catch (ClientProtocolException e) {
    69             e.printStackTrace();
    70             log.error(e.getMessage());
    71         } catch (IOException e) {
    72             e.printStackTrace();
    73             log.error(e.getMessage());
    74         } finally {
    75             httpClient.getConnectionManager().shutdown(); 
    76         }
    77         return result;
    78     }

    4.4 版本

     1     /**
     2      * url 请求 paramUrl
     3      * 
     4      * @time 2015年11月10日下午4:40:22
     5      * @packageName com.rom.utils
     6      * @param url         url请求地址
     7      * @param header    请求头信息
     8      * @param params     url请求参数
     9      * @param paramstype 参数类型 1:json格式 ; 其他:正常格式;
    10      * @return
    11      */
    12     public  static JSONObject paramUrl(String url ,Map<String, String> header,
    13             Map<String, Object> params,String paramstype){
    14         
    15         RequestConfig config = RequestConfig.custom()
    16                 .setConnectionRequestTimeout(10000).setConnectTimeout(10000)
    17                 .setSocketTimeout(10000).build();
    18         
    19         HttpPost httpPost = new HttpPost(url);
    20         
    21         httpPost.setConfig(config);
    22         if (header != null) {
    23             Set<String> headerkey = header.keySet();
    24             Iterator<String> headerkeyit = headerkey.iterator();
    25             while (headerkeyit.hasNext()) {
    26                 String key = (String) headerkeyit.next();
    27                 httpPost.setHeader(key, header.get(key).toString());
    28             }
    29         }
    30         
    31         if (params != null) {
    32             // 如果参数类型为1 证明参数传递方式为 json格式
    33             if (paramstype != null && paramstype.equals("1")) {
    34                 StringEntity entity = new StringEntity(JSONObject.fromObject(params).toString(), "utf-8");// 解决中文乱码问题
    35                 entity.setContentEncoding("UTF-8");
    36                 entity.setContentType("application/json");
    37                 httpPost.setEntity(entity);
    38             } else {
    39                 // 参数格式为 键值对形式
    40                 List<NameValuePair> formparams = new ArrayList<NameValuePair>();
    41                 Set<String> keySet = params.keySet();
    42                 Iterator<String> keyit = keySet.iterator();
    43                 while (keyit.hasNext()) {
    44                     String key = (String) keyit.next();
    45                     formparams.add(new BasicNameValuePair(key, params.get(key).toString()));
    46                 }
    47                 UrlEncodedFormEntity uefEntity = null;
    48                 try {
    49                     uefEntity = new UrlEncodedFormEntity(formparams, "UTF-8");
    50                 } catch (UnsupportedEncodingException e) {
    51                     e.printStackTrace();
    52                 }
    53                 httpPost.setEntity(uefEntity);
    54             }
    55         }
    56         
    57         CloseableHttpClient httpClient = HttpClients.custom().setDefaultRequestConfig(null).build();//设置进去
    58         
    59         CloseableHttpResponse response = null;
    60         StringBuffer out;
    61         try {
    62             response = httpClient.execute(httpPost); 
    63             
    64             HttpEntity entity = response.getEntity();
    65             
    66             InputStream in = entity.getContent();
    67             
    68             out = new StringBuffer();  
    69             byte[] b = new byte[4096];  
    70             for (int n; (n = in.read(b)) != -1;) {  
    71                 out.append(new String(b, 0, n));  
    72             }
    73             JSONObject json = JSONObject.fromObject(out.toString());
    74             in = null;
    75             return json;
    76         } catch (ClientProtocolException e) {
    77             log.error(e.getMessage());
    78         } catch (IllegalStateException e) {
    79             log.error(e.getMessage());
    80         } catch (IOException e) {
    81             log.error(e.getMessage());
    82         } finally{
    83             try {
    84                 if (httpClient!=null) {
    85                     httpClient.close();
    86                 }
    87             } catch (IOException e) {
    88                 log.error(e);
    89             }
    90         }
    91         return null;
    92     }
    93     
  • 相关阅读:
    把excel导入的自定义时间改成yyyyMMdd
    sql多表查询时怎么获取查到的字段
    select 和 input 的不可编辑,input隐藏
    关闭dialog(lhgdialog)
    ajax 和 post 传多个参数值具体怎么写
    弹出提示框,对话框
    function方法中this的用法
    combobox获取值
    easyui-panel 滚动条禁用
    字符串用逗号隔开;有逗号的字符串遍历
  • 原文地址:https://www.cnblogs.com/zfy0098/p/5564733.html
Copyright © 2011-2022 走看看