httpClient 接口调用

<dependency>
   <groupId>commons-httpclient</groupId>
   <artifactId>commons-httpclient</artifactId>
   <version>3.1</version>

</dependency>





/** 
* @Title: postRequestResponseBodyAsString 
* @Description: http post请求,application/x-www-form-urlencoded方式
* @param url
* @param parameters
* @param contimeout
* @param sotimeout
* @return
* @throws Exception
* @throws 
*/ 
public static String postRequestResponseBodyAsString(String url, Map<String, String> parameters,Integer contimeout,Integer sotimeout) throws Exception {
HttpClient httpClient = new HttpClient();
httpClient.getHttpConnectionManager().getParams().setSoTimeout(sotimeout);
httpClient.getHttpConnectionManager().getParams().setConnectionTimeout(contimeout);
// httpClient.getHostConfiguration().setProxy("proxy2.fn.com",8080);
PostMethod method = new PostMethod(url);
int statusCode;
String responseCharSet ="";
String responseString = "";
try {
for (String key : parameters.keySet()) {
method.addParameter(key, parameters.get(key));
}
method.addRequestHeader("Content-Type", "application/x-www-form-urlencoded;charset=UTF-8");
statusCode = httpClient.executeMethod(method);
if (statusCode != HttpStatus.SC_OK) {
LOG.error(">>>>>>>>>>>>>>>  Method failed: " + method.getStatusLine());
LOG.error(">>>>>>>>>>>>>>>  Http服务链路异常:服务器状态码为" + statusCode);
}
responseCharSet = method.getResponseCharSet();
responseString = method.getResponseBodyAsString();
if ("ISO-8859-1".equals(responseCharSet)) {
responseString = new String(responseString.getBytes(responseCharSet), "UTF-8");
}


} catch (Exception e) {
LOG.error(">>>>>>>>>>>>>>>  Http服务链路异常:" + e.getMessage() + e);
} finally {
method.releaseConnection();
}
return responseString;
}




public static String postRequestResponseBodyAsString(String url, String str,Integer contimeout,Integer sotimeout) throws Exception{
        HttpClient httpClient = new HttpClient();
        httpClient.getHttpConnectionManager().getParams().setSoTimeout(sotimeout);
        httpClient.getHttpConnectionManager().getParams().setConnectionTimeout(contimeout);
        PostMethod method = new PostMethod(url);
        int statusCode;
        String responseCharSet ="";
        String responseString = "";
        try {
//            String json = JSON.toJSONString(o);
            StringRequestEntity entity = new StringRequestEntity(str,"application/json","utf-8");//解决中文乱码问题
            method.setRequestEntity(entity);
            method.addRequestHeader("Content-Type", "application/json;charset=UTF-8");
            statusCode = httpClient.executeMethod(method);
            if (statusCode != HttpStatus.SC_OK) {
                LOG.error(">>>>>>>>>>>>>> Method failed: " + method.getStatusLine());
                LOG.error(">>>>>>>>>>>>>> Http服务链路异常:服务器状态码为" + statusCode);
            }
            responseCharSet = method.getResponseCharSet();
            responseString = method.getResponseBodyAsString();
            if ("ISO-8859-1".equals(responseCharSet)) {
                responseString = new String(responseString.getBytes(responseCharSet), "UTF-8");
            }


        } catch (Exception e) {
            LOG.error(">>>>>>>>>>>>>>  Http服务链路异常:" + e.getMessage() + e);
        } finally {
            method.releaseConnection();
        }
        return responseString;
    }


    



  /** 
    * @Title: uploadFile 
    * @Description: 上传文件
    * @param file 文件
    * @param url 上传地址
    * @param map header-type
    * @return
    * @throws 
    */ 
    public static String uploadFile(File file, String url,Map<String, String> map) {
        if (!file.exists()) {
            return null;
        }
        PostMethod postMethod = new PostMethod(url);
        String responseCharSet ="";
        String responseString = "";
        try {
            // FilePart:用来上传文件的类
            FilePart fp = new CustomFilePart("filedata", file);
            Part[] parts = { fp };
            postMethod.getParams().setParameter(HttpMethodParams.HTTP_CONTENT_CHARSET,FilePart.DEFAULT_CHARSET); 
            // 对于MIME类型的请求,httpclient建议全用MulitPartRequestEntity进行包装
            MultipartRequestEntity mre = new MultipartRequestEntity(parts,
                    postMethod.getParams());
            postMethod.setRequestEntity(mre);
            for (String key : map.keySet()) {
                postMethod.addRequestHeader(key, map.get(key));
            }
             
            HttpClient httpClient = new HttpClient();
            httpClient.getHttpConnectionManager().getParams().setSoTimeout(20000);
            httpClient.getHttpConnectionManager().getParams().setConnectionTimeout(8000);
            int status = httpClient.executeMethod(postMethod);
            responseCharSet = postMethod.getResponseCharSet();
            responseString = postMethod.getResponseBodyAsString();
            
            if (status == HttpStatus.SC_OK) {
                if ("ISO-8859-1".equals(responseCharSet)) {
                    responseString = new String(responseString.getBytes(responseCharSet), "UTF-8");
                }
            } else {
                LOG.error(">>>>>>>>>>>>>> Method failed: " + postMethod.getStatusLine());
                LOG.error(">>>>>>>>>>>>>> Http服务链路异常:服务器状态码为" + status);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            // 释放连接
            postMethod.releaseConnection();
        }
        return responseString;
    }
    
    /** 
    * @Title: getFile 
    * @Description: 拿到文件内容
    * @param url 地址
    * @param charset 编码
    * @return
    * @throws 
    */ 
    public static String getFile(String url,Charset charset) {
        return getFile(url, charset,8000,20000);
    }
    public static String getFile(String url,Charset charset,Integer contimeout,Integer sotimeout) {
        GetMethod method = new GetMethod(url);
        String responseCharSet ="";
        String responseString = "";
        try {
            HttpClient httpClient = new HttpClient();
            httpClient.getHttpConnectionManager().getParams().setSoTimeout(sotimeout);
            httpClient.getHttpConnectionManager().getParams().setConnectionTimeout(contimeout);
            int status = httpClient.executeMethod(method);
            responseCharSet = method.getResponseCharSet();
            responseString = method.getResponseBodyAsString();
            if (status == HttpStatus.SC_OK) {
                if ("ISO-8859-1".equals(responseCharSet)) {
                    responseString = new String(responseString.getBytes(responseCharSet), charset);
                }
            } else {
                LOG.error(">>>>>>>>>>>>>> Method failed: " + method.getStatusLine());
                LOG.error(">>>>>>>>>>>>>> Http服务链路异常:服务器状态码为" + status);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            // 释放连接
            method.releaseConnection();
        }
        return responseString;
    }




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值