一、更换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. sqoop从oracle数据库抽取数据,导入到hive

    环境: hadoop-2.7.5 sqoop-1.4.7 zookeeper-3.4.10 hive-2.3.3 (使用mysql配置元数据库) jdk1.8.0_151 oracle 11.2.0. ...

  2. 与你一起学习MS Project——基础篇:Project基础应用

    为了更清晰容易地熟悉掌握Project的基础应用,我们在基础篇中一起来学习掌握在Project中如何做进度计划.资源计划.成本计划以及跟踪项目的执行情况并生成所需的项目报表. 一.进度计划 这里,首先 ...

  3. django-获取购物车商品数量-redis

    视图函数views.py中 from django_redis import get_redis_connection # 连接redis class IndexView(View): '''首页'' ...

  4. cube.js 新版本试用preosto

    cube.js 新的版本添加了更多的数据库的支持,但是目前cubejs-cli 以及官方文档问题还挺多,使用不清晰,文档有明显的错误 以下演示presto 数据库的使用 环境准备 安装新版本的cube ...

  5. Using HAProxy as an API Gateway, Part 3 [Health Checks]

    转自:https://www.haproxy.com/blog/using-haproxy-as-an-api-gateway-part-3-health-checks/ Achieving high ...

  6. lettcode 上的几道哈希表与链表组合的数据结构题

    目录 LRU缓存 LFU缓存 全O(1)的数据结构 lettcode 上的几道哈希表与链表组合的数据结构题 下面这几道题都要求在O(1)时间内完成每种操作. LRU缓存 LRU是Least Recen ...

  7. [RN] React Native 使用 FlatList 实现九宫格布局 GridList

    React Native 使用 FlatList 实现九宫格布局 先看图片演示实例: 本文以图片列表为例,实现九宫格布局! 主要有两种方法: 1)方法一: 利用FlatList的 numColumns ...

  8. c语言中一种典型的排列组合算法

    c语言中的全排列算法和组合数算法在实际问题中应用非常之广,但算法有许许多多,而我个人认为方法不必记太多,最好只记熟一种即可,一招鲜亦可吃遍天 全排列: #include<stdio.h> ...

  9. rust数据类型

    fn main() { //char支持4个字节,支持emoji let jp = "ゆ"; let emoji = "✨"; let ch = "囧 ...

  10. go -- go 程序 启动docker容器

    package main import ( "io" "log" "os" "time" "github.co ...