一、更换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. 域权限维持:Ptk(Pass The Ticket)

    Kerberos协议传送门:https://www.cnblogs.com/zpchcbd/p/11707302.html 1.黄金票据 2.白银票据

  2. L1434滑雪

    一,看题 1,这个长度怎么算的. 从它自己数,可以走下去的位置. 2,这个题的衣服怎么披上去呀. 3,搜索目标,状态. 肯定要用坐标,不然怎么搜索. 4,在前期还是多写把. 5,我靠这个点还是随机的& ...

  3. JS 不声明第三个变量的情况下实现两数变换

    1. var a = 1; var b = 2; a = a + b; b = a - b; a = a - b; console.log(a); console.log(b); 2. var a = ...

  4. RaiseFailFastException函数

    引发绕过所有异常处理程序(基于帧或矢量)的异常.引发此异常将终止应用程序并调用Windows错误报告(如果Windows错误报告正在运行). 原型: VOID WINAPI RaiseFailFast ...

  5. HTML5 文件读取

    一.定义 input的file类型会渲染为一个按钮和一段文字.点击按钮可打开文件选择窗口,文字表示对文件的描述(大部分情况下为文件名):file类型的input会有files属性,保存着文件的相关信息 ...

  6. 深度讨论i++问题

    例题1:下列程序的输出结果是多少? public class Test { static { int x = 5; } static int x, y; public static void main ...

  7. 如果判断条件过多,可以直接在computed里面去返回需要判断的数据

    bad <div class="offer-item_margin" v-show="offer.supplierName || offer.supplierSto ...

  8. better-scroll在vue项目中的使用

    1.准备工作 在项目中安装better-scroll: npm install --save better-scroll 组件中引入插件 import BScroll from "bette ...

  9. 作业——12 hadoop大作业

    作业的要求来自于:https://edu.cnblogs.com/campus/gzcc/GZCC-16SE2/homework/3339 Hadoop综合大作业 1.以下是爬虫大作业产生的csv文件 ...

  10. 【大数据】安装关系型数据库MySQL安装大数据处理框架Hadoop

    作业来源于:https://edu.cnblogs.com/campus/gzcc/GZCC-16SE2/homework/3161 1. 简述Hadoop平台的起源.发展历史与应用现状. 列举发展过 ...