一、更换jar方法。

1、将httpClient4.5.1.jar包去掉,更换使用commons-httpclient-3.1.jar。

2、更换方法,将HttpPost类转换为PostMethod类。

3、使用httpClient4.5.1的类

import java.security.GeneralSecurityException;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;

import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSession;

import org.apache.commons.io.IOUtils;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
import org.apache.http.conn.ssl.TrustStrategy;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
import org.apache.http.ssl.SSLContextBuilder;

public class HttpClientSSLUtils
{
private static HttpClient client = null;

protected static final Integer DEFAULT_CONNECTION_TIME_OUT = Integer.valueOf(100000);

protected static final Integer DEFAULT_SOCKET_TIME_OUT = Integer.valueOf(200000);
protected static final String DEFAULT_CHAR_SET = "UTF-8";

public static String doPost(String url, String jsonText)
throws Exception
{
HttpClient client = null;
HttpPost post = new HttpPost(url);
try {
if ((jsonText != null) && (!(jsonText.isEmpty()))) {
StringEntity entity = new StringEntity(jsonText, ContentType.APPLICATION_JSON);
post.setEntity(entity);
}

RequestConfig.Builder customReqConf = RequestConfig.custom();
customReqConf.setConnectTimeout(DEFAULT_CONNECTION_TIME_OUT.intValue());
customReqConf.setSocketTimeout(DEFAULT_CONNECTION_TIME_OUT.intValue());
post.setConfig(customReqConf.build());
HttpResponse res = null;
if (url.startsWith("https"))
{
client = createSSLInsecureClient();
res = client.execute(post);
}
else {
client = createSSLInsecureClient();;
res = client.execute(post);
}
String str = IOUtils.toString(res.getEntity().getContent(), "UTF-8");

return str;
}
finally
{
post.releaseConnection();
if ((url.startsWith("https")) && (client != null) && (client instanceof CloseableHttpClient))
((CloseableHttpClient)client).close();
}
}

public static String doGet(String url)
throws Exception
{
HttpClient client = null;
HttpGet get = new HttpGet(url);
String result = "";
try
{
RequestConfig.Builder customReqConf = RequestConfig.custom();
customReqConf.setConnectTimeout(DEFAULT_CONNECTION_TIME_OUT.intValue());
customReqConf.setSocketTimeout(DEFAULT_CONNECTION_TIME_OUT.intValue());
get.setConfig(customReqConf.build());
HttpResponse res = null;
if (url.startsWith("https"))
{
client = createSSLInsecureClient();
res = client.execute(get);
}
else {
client = client;
res = client.execute(get);
}
result = IOUtils.toString(res.getEntity().getContent(), "UTF-8");
} finally {
get.releaseConnection();
if ((url.startsWith("https")) && (client != null) && (client instanceof CloseableHttpClient)) {
((CloseableHttpClient)client).close();
}
}
return result;
}

private static CloseableHttpClient createSSLInsecureClient()
throws GeneralSecurityException
{
try
{
SSLContext sslContext = new SSLContextBuilder().loadTrustMaterial(null, new TrustStrategy()
{
public boolean isTrusted(X509Certificate[] chain, String authType) throws CertificateException {
return true;
}

}).build();
SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(sslContext, new HostnameVerifier()
{
public boolean verify(String hostname, SSLSession session)
{
return true;
}

});
return HttpClients.custom().setSSLSocketFactory(sslsf).build();
} catch (GeneralSecurityException e) {
throw e;
}
}

/* public static void main(String[] args)
{
String url = "https://10.33.39.8/webapi/service/base/getPlatAuthSubSystemList";
String params = "appkey=appkey&time=" + System.currentTimeMillis() + "&pageNo=1&pageSize=10";

String urlString = url + "?" + params + "&token=" +
Digests.buildToken(new StringBuilder().append(url).append("?").append(params).toString(), null, "*****************");
try
{
String output = new String(doGet(urlString));
System.out.println(output);
} catch (Exception e) {
e.printStackTrace();
}

url = "https://10.20.134.21/webapi/service/base/addPlatCard";
Map map = new HashMap();
map.put("appkey", "*****");
map.put("time", Long.valueOf(System.currentTimeMillis()));
map.put("startCardNo", "16000");
map.put("endCardNo", "16010");
params = JsonUtils.object2Json(map);
url = url + "?token=" + Digests.buildToken(new StringBuilder().append(url).append("?").append(params).toString(), null, "******");
try {
System.out.println(doPost(url, params));
} catch (Exception e) {
e.printStackTrace();
}
}*/

static
{
PoolingHttpClientConnectionManager cm = new PoolingHttpClientConnectionManager();
cm.setMaxTotal(128);
cm.setDefaultMaxPerRoute(128);
client = HttpClients.custom().setConnectionManager(cm).build();
}
}

4、使用commons-httpclient-3.1.jar的类(以json字符串为例传输数据)

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;

public class testHttp {
public static String sendHttpReq(String url,String params) throws Exception {

String encode = "UTF-8";
HttpClient client = new HttpClient();
PostMethod method = new PostMethod(url);
RequestEntity se = new StringRequestEntity (params ,"application/json" ,"UTF-8");
method.setRequestEntity(se);
method.setRequestHeader("Content-Type","application/json");
client.executeMethod(method);
String aa=new String(method.getResponseBody(), "UTF-8");
System.out.println(aa);
return new String(method.getResponseBody(), encode);
}

}

was8.5调用HttpPost使用httpClient-4.5.1.jar与was原生自带jar包冲突的更多相关文章

  1. soap调用Jar包冲突,SOAPMessageContext

    ================================ ©Copyright 蕃薯耀 2020-01-07 https://www.cnblogs.com/fanshuyao/ soap调用 ...

  2. maven项目,httpclient jar包冲突

    包含httpclient的jar包 org.apache.thrift:libthrift org.jboss.resteasy:resteasy-jaxrs com.alibaba:dubbo ma ...

  3. nRF51822 配对之device_manager_init 调用,以及保证 用户数据存储 的Flash 操作不与device manager 模块冲突

    昨天 遇到了一个烦心的问题,被老外客户怼了两句,恼火,很想发火,发现英文不够用,算了,就不跟直肠的鬼佬一般见识.说正事. 最近的一个nRF51822+MT2503 钱包防丢项目,准备接近量产了.昨天做 ...

  4. was(websphere application server)中用apache的httpclient时jar包冲突问题的解决

    这个问题可以用was的共享库解决. 具体解决方案如下图所示: 对于有多个jar包冲突时,为每个冲突的jar包都新建一个共享库即可. 我之前的错误操作是以为一个共享库可以添加多个冲突的jar包用分号和逗 ...

  5. 20180831_jar包冲突2_天安微信httpclient冲突

    一.异常现象 微信项目需要向腾讯服务器发送请求获取token. 但是在请求的时候抛了个异常: <2018-8-30 下午05时39分18秒 CST> <Notice> < ...

  6. 开源中国iOS客户端学习

    开源中国iOS客户端学习 续写前言 <开源中国iOS客户端学习>续写前系列博客    http://blog.csdn.net/column/details/xfzl-kykhd.html ...

  7. Android开发错误汇总

    [错误信息] [2011-01-19 16:39:10 - ApiDemos] WARNING: Application does not specify an API level requireme ...

  8. .Net程序员学用Oracle系列(7):视图、函数、过程、包

    <.Net程序员学用Oracle系列:导航目录> 本文大纲 1.视图 1.1.创建视图 2.函数 2.1.创建函数 2.2.调用函数 3.过程 3.1.创建过程 3.2.调用过程 4.包 ...

  9. .Net程序员学用Oracle系列(7):视图、函数、存储过程、包

    1.视图 1.1.创建.删除及调用普通视图 1.2.高级视图介绍 2.函数 2.1.系统函数介绍 2.2.创建.删除及调用自定义函数 3.存储过程 3.1.创建.修改及删除存储过程 3.2.调用存储过 ...

随机推荐

  1. width: calc(100% - 80px); 屏幕自适应方法

    width: calc(100% - 80px); 屏幕自适应方法

  2. MongoDB 4.2 的主要亮点(转载)

    在6月份召开的MongoDB全球用户大会上, MongoDB官宣了MongoDB Server 4.2,在经过100,000多个运行实例的测试后,MongoDB 4.2表现强劲.现在4.2版本正式上线 ...

  3. OKR的两个基本原则

    <启示录>作者,前易贝高级副总裁,硅谷产品集团创始人马蒂·卡根在<OKR工作法>的序言中提到了目标管理法的两个原则: 不要告诉下属具体怎么做,要告诉他们你要什么,他们就会给你满 ...

  4. CSS行内元素

    一.典型代表 span a ,strong em del, ins 二.特点: 在一行上显示 不能直接设置宽高 元素的宽和高就是内容撑开的宽高. <style type="text/c ...

  5. 洛谷P1081 开车旅行

    题目 双向链表+倍增+模拟. \(70pts\): 说白了此题的暴力就是细节较多的模拟题. 我们设离\(i\)城市最近的点的位置为\(B[i]\),第二近的位置为\(A[i]\).设\(A\)或\(B ...

  6. [HAOI2018]染色(NTT)

    前置芝士 可重集排列 NTT 前置定义 \[\begin{aligned}\\ f_i=C_m^i\cdot \frac{n!}{(S!)^i(n-iS)!}\cdot (m-i)^{n-iS}\\ ...

  7. C++2.0新特性(六)——<Smart Pointer(智能指针)之shared_ptr>

    Smart Pointer(智能指针)指的是一类指针,并不是单一某一个指针,它能知道自己被引用的个数以至于在最后一个引用消失时销毁它指向的对象,本文主要介绍C++2.0提供的新东西 一.Smart P ...

  8. vue data不可以使用箭头函数的问题解析

    这篇文章主要介绍了vue data不可以使用箭头函数问题,本文通过源码解析给大家介绍的非常详细,具有一定的参考借鉴价值,需要的朋友可以参考下 首先需要明确,a() {}和 b: () => {} ...

  9. openstack重设虚拟机实例密码

    目录结构: 引出 采用 nova get-password 方式 采用 libvirt-set-admin-password 采用 nova rebuild instance 的方式 采用 cloud ...

  10. ubuntu16上部署confluence-6.14.5的迁移

    author:headsen chen date:  2019-10-18  15:02:06 notice :created  by  headsen chen himself and not al ...