• org.apache.commons.httpclient


    org.apache.commons.httpclient

     1   /**
     2      * post 方法
     3      * @param  url
     4      * @param params
     5      * @return
     6      */
     7     public static String post(String url, Object content, String encode) throws Exception {
     8         
     9         byte[] responseBody = null;
    10         HttpClient httpclient = new HttpClient();
    11         PostMethod httpPost = new PostMethod(url);
    12         // 设置连接超时时间(单位毫秒)         
    13         httpclient.getHttpConnectionManager().getParams().setConnectionTimeout(60000);
    14         // 设置读数据超时时间(单位毫秒)        
    15         httpclient.getHttpConnectionManager().getParams().setSoTimeout(60000);
    16         try {
    17             httpPost.getParams().setParameter(HttpMethodParams.RETRY_HANDLER,new DefaultHttpMethodRetryHandler(3, false));
    18             // servlet
    19             if (content instanceof Map) {
    20                 @SuppressWarnings("unchecked")
    21                 Map<String, String> map = (Map<String, String>)content;
    22                 NameValuePair[] param = new NameValuePair[map.size()];
    23                 
    24                 int index = 0;
    25                 for (Map.Entry<String, String> entry : map.entrySet()) {
    26                     param[index] = new NameValuePair(entry.getKey(),URLEncoder.encode(entry.getValue(), "GBK"));
    27                 }
    28                 
    29                 httpPost.setRequestBody(param);
    30             }
    31             // rest
    32             else {
    33                 httpPost.setRequestEntity(new StringRequestEntity((String)content,"plain/text", encode));
    34             }
    35             
    36             // post
    37             int statusCode = httpclient.executeMethod(httpPost);
    38             // success
    39             if (statusCode == HttpStatus.SC_OK) {
    40                 responseBody = httpPost.getResponseBody();
    41             }
    42             // failure
    43             else {
    44                 
    45             }
    46         } catch (HttpException e) {
    47             throw new Exception(e.getMessage());
    48         } catch (IOException e) {
    49             throw new Exception(e.getMessage());
    50         } catch (Exception e) {
    51             throw new Exception(e.getMessage());
    52         } finally {
    53             httpPost.releaseConnection();
    54         }
    55         return new String(responseBody, encode);
    56     }
  • 相关阅读:
    Satellite Basics
    Antenna basics
    Installing a Sailor 900 VSAT
    Installing Intellian VSAT
    IPTV、DVB、OTT的区别
    STM32两种下载方式
    Postman 使用指南
    elasticsearch term 查询之一
    Elasticsearch -from + size设置
    TIMESTAMP和DATETIME的区别
  • 原文地址:https://www.cnblogs.com/xiluhua/p/5136251.html
Copyright © 2020-2023  润新知