httpclient 进行post访问

方式一:commons-httpclient-3.1.jar

import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.methods.RequestEntity;
import org.apache.commons.httpclient.methods.StringRequestEntity;
import org.apache.commons.httpclient.params.HttpMethodParams;

private  String requestTwo(String url,Map<String,String> map) throws Exception{
    HttpClient httpClient = new HttpClient();
    httpClient.getHttpConnectionManager().getParams().setConnectionTimeout(15000);
    PostMethod postMethod = new PostMethod(url);
    postMethod.getParams().setParameter(HttpMethodParams.SO_TIMEOUT, 60000);
    postMethod.addRequestHeader("Content-Type", "application/json");
    try {
    	String str = JSONObject.fromObject(map).toString();
    	RequestEntity entity = new StringRequestEntity(str, "application/json", "UTF-8");
        postMethod.setRequestEntity(entity);

        httpClient.executeMethod(postMethod);
        String result = postMethod.getResponseBodyAsString();
        postMethod.releaseConnection();
        return result;
    } catch (Exception e) {
    	e.printStackTrace();
    }
	return null;
}

方式二:httpclient-4.5.2.jar

import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.impl.client.LaxRedirectStrategy;
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
import org.apache.http.util.EntityUtils;

private String requestTwo(String url,Map<String,String> map) throws Exception{
	/** 请求上游  **/
    HttpPost post = new HttpPost(url);
    post.setHeader("Content-Type", "application/json;");
    String str = JSONObject.fromObject(map).toString();
    StringEntity entity = new StringEntity(URLEncoder.encode(str,"UTF8"));
    post.setEntity(entity);
    RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(30000).setSocketTimeout(30000).build();
    PoolingHttpClientConnectionManager cm = new PoolingHttpClientConnectionManager();
    cm.setMaxTotal(200);
    cm.setDefaultMaxPerRoute(50);
    HttpClient ht = HttpClients.custom().setConnectionManager(cm).setDefaultRequestConfig(requestConfig).setRedirectStrategy(new LaxRedirectStrategy()).build();
    HttpResponse response = ht.execute(post);
    /** 返回结果处理  **/
    String resp = new String();
    if (response != null) {
        resp = EntityUtils.toString(response.getEntity());
    }
    return resp;
}

以上两种方式是通过不同jar包实现访问,使用前请先确认是否有对应jar文件。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值