一、更换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. 转 OJDBC驱动版本区别 [ojdbc14.jar,ojdbc5.jar跟ojdbc6.jar的区别]

    OJDBC版本区别 [ojdbc14.jar,ojdbc5.jar和ojdbc6.jar的区别] 在使用Oracle JDBC驱动时,有些问题你是不是通过替换不同版本的Oracle  JDBC驱动来解 ...

  2. 9、Hadoop配置文件和HDFS垃圾回收

    配置文件 默认配置文件:相对应的jar包中 core-default.xml hdfs-default.xml yarn-default.xml mapred-default.xml 自定义配置文件 ...

  3. webpack的loader的原理和实现

    想要实现一个loader,需要首先了解loader的基本原理和用法. 1. 使用 loader是处理模块的解析器. module: { rules: [ { test: /\.css$/, use: ...

  4. S1_搭建分布式OpenStack集群_12 界面horizon安装

    一.界面的安装控制节点安装软件包:# yum install openstack-dashboard -y 修改配置文件:# vim /etc/openstack-dashboard/local_se ...

  5. 【JZOJ6218】【20190615】卖弱

    题目 题解 我写的另一种方法,复杂度是\(O(Tm+nm)\)的,这是huangzhaojun写的题解... #include<cstring> #include<cstdio> ...

  6. Cocos CreatorUI系统上

    若本号内容有做得不到位的地方(比如:涉及版权或其他问题),请及时联系我们进行整改即可,会在第一时间进行处理. 请点赞!因为你们的赞同/鼓励是我写作的最大动力! 欢迎关注达叔小生的简书! 这是一个有质量 ...

  7. shell脚本编程基础之自定义函数库

    脚本编程知识点 ${#VAR_NAME}:引用变量中字符的长度 A="25 90 100 120": echo ${A#* }:针对A变量,#表示从左往右,*空格表示以空格为分隔符 ...

  8. python3 Paramiko模块学习

    简介 ssh是一个协议,OpenSSH是其中一个开源实现,paramiko是Python的一个库,实现了SSHv2协议(底层使用cryptography). 有了Paramiko以后,我们就可以在Py ...

  9. Atcoder Regular Contest 060 F题第一问答案证明

    一切的开始 令 \(x\) 为字符串,\(p\) 为正整数.如果对于满足 \(0\le i<|x|−p\) 的任何整数 \(i\) 满足 \(x[i]=x[i+p]\),则 \(p\) 称为 \ ...

  10. [Beta]第十次 Scrum Meeting

    [Beta]第十次 Scrum Meeting 写在前面 会议时间 会议时长 会议地点 2019/5/20 22:00 20min 大运村公寓6F寝室 附Github仓库:WEDO 例会照片 工作情况 ...