Coder Social home page Coder Social logo

arronlong / httpclientutil Goto Github PK

View Code? Open in Web Editor NEW
1.4K 118.0 720.0 229 KB

该项目基于HttpClient-4.4.1封装的一个工具类。提供了更为简单的GET、POST及上传下载等功能。同时支持插件式配置Http-Header(包含自定义header)、配置SSL和Proxy等。

Home Page: https://blog.csdn.net/xiaoxian8023/category_9264781.html

License: Apache License 2.0

Java 99.90% Batchfile 0.10%

httpclientutil's Issues

发送超大文本数据的时候报错

private static HttpClient client= HCB.custom().timeout(30000*10).build();

{"Code":"xxxxxxx","Date":1450423000000,"dname":"二手","qvpn":"685"} 25000 条。

return HttpClientUtil.send(client,SystemConfiguration.getString("upload.system.api"), paramMap, context, "utf-8");

com.tgb.ccl.http.exception.HttpProcessException: java.net.SocketException: Connection reset by peer: socket write error
at com.tgb.ccl.http.httpclient.HttpClientUtil.execute(HttpClientUtil.java:1536)
at com.tgb.ccl.http.httpclient.HttpClientUtil.send(HttpClientUtil.java:1266)
at com.tgb.ccl.http.httpclient.HttpClientUtil.send(HttpClientUtil.java:999)
at com.tgb.ccl.http.httpclient.HttpClientUtil.send(HttpClientUtil.java:969)

connection reset error in https request

HttpConfig config = HttpConfig.custom()
.headers(headers)
.url(iamtokenUrl)
.encoding("utf-8")
.client(HCB.custom().sslpv(SSLs.SSLProtocolVersion.TLSv1_2).ssl().build())
.json(JSON.toJSONString(map));
String result = HttpClientUtil.post(config);

ERROR MESSAGE:
com.arronlong.httpclientutil.exception.HttpProcessException: java.net.SocketException: Connection reset
at com.arronlong.httpclientutil.HttpClientUtil.execute(HttpClientUtil.java:478)
at com.arronlong.httpclientutil.HttpClientUtil.send(HttpClientUtil.java:380)
at com.arronlong.httpclientutil.HttpClientUtil.post(HttpClientUtil.java:131)
at com.caacitc.tcdm.business.imoc.domain.service.impl.ImocOrderImpl.getIamToken(ImocOrderImpl.java:68)

调用delete传参有示例吗,这样写是否可行?

	Header[] headers=HttpHeader.custom().userAgent("Mozilla/5.0")
			.contentType(Headers.APP_FORM_URLENCODED).build();  
	HttpConfig config = HttpConfig.custom().headers(headers).url(“https://localhost/message/template.json?appid=12993&signature=a3fcb88e97894ca0674b5a00480f3aa6&template_id=gK0hk2”).encoding("utf-8");
	String res = HttpClientUtil.send(config.method(HttpMethods.DELETE));
	System.out.println(res);

对方为PHP服务器导致请求体空

问题描述:
使用这个请求框架很方便构造,但是遇到了以下的问题(对方服务器后台语言采用PHP):
1.本地windows、本地mac都能请求通并且对方服务器能够正常返回。
2.但是扔到阿里云服务器,对方接收到的请求体就为空,试过对接过很多甲方都是这样。但是甲方使用java语言就能正常使用。

请问这是啥问题。

发现了一些问题

1 SSLs认证使用同步锁,导致开启多个线程实际上只有一个线程在执行。
解决:public SSLConnectionSocketFactory getSSLCONNSF(SSLProtocolVersion sslpv) 取消同步锁
2 请求没设置超时时间。
解决:HttpClientUtil 类中 execute(HttpConfig config)增加超市时间
//创建请求对象
HttpRequestBase request = getRequest(config.url(), config.method());
request.setConfig(RequestConfig.custom().setCircularRedirectsAllowed(true).build());
//设置header信息
request.setHeaders(config.headers());
request.setConfig(RequestConfig.custom().setConnectionRequestTimeout(5000).build());
request.setConfig(RequestConfig.custom().setConnectTimeout(5000).build());

3 代理没设置正确

HCB 类设置代理添加 setProxy(proxy)
public HCB proxy(String hostOrIP, int port) {
// 依次是代理地址,代理端口号,协议类型
HttpHost proxy = new HttpHost(hostOrIP, port, "http");
setProxy(proxy);
DefaultProxyRoutePlanner routePlanner = new DefaultProxyRoutePlanner(proxy);
return (HCB) this.setRoutePlanner(routePlanner);
}

Caused by: javax.net.ssl.SSLException: Received fatal alert: protocol_version

Exception in thread "main" com.arronlong.httpclientutil.exception.HttpProcessException: javax.net.ssl.SSLException: Received fatal alert: protocol_version
at com.arronlong.httpclientutil.HttpClientUtil.execute(HttpClientUtil.java:433)
at com.arronlong.httpclientutil.HttpClientUtil.send(HttpClientUtil.java:374)
at com.arronlong.httpclientutil.HttpClientUtil.get(HttpClientUtil.java:102)
at com.arronlong.httpclientutil.test.Demo.main(Demo.java:30)
Caused by: javax.net.ssl.SSLException: Received fatal alert: protocol_version
at sun.security.ssl.Alerts.getSSLException(Unknown Source)
at sun.security.ssl.Alerts.getSSLException(Unknown Source)
at sun.security.ssl.SSLSocketImpl.recvAlert(Unknown Source)
at sun.security.ssl.SSLSocketImpl.readRecord(Unknown Source)
at sun.security.ssl.SSLSocketImpl.performInitialHandshake(Unknown Source)
at sun.security.ssl.SSLSocketImpl.startHandshake(Unknown Source)
at sun.security.ssl.SSLSocketImpl.startHandshake(Unknown Source)
at org.apache.http.conn.ssl.SSLConnectionSocketFactory.createLayeredSocket(SSLConnectionSocketFactory.java:394)
at org.apache.http.conn.ssl.SSLConnectionSocketFactory.connectSocket(SSLConnectionSocketFactory.java:353)
at org.apache.http.impl.conn.DefaultHttpClientConnectionOperator.connect(DefaultHttpClientConnectionOperator.java:134)
at org.apache.http.impl.conn.PoolingHttpClientConnectionManager.connect(PoolingHttpClientConnectionManager.java:353)
at org.apache.http.impl.execchain.MainClientExec.establishRoute(MainClientExec.java:380)
at org.apache.http.impl.execchain.MainClientExec.execute(MainClientExec.java:236)
at org.apache.http.impl.execchain.ProtocolExec.execute(ProtocolExec.java:184)
at org.apache.http.impl.execchain.RetryExec.execute(RetryExec.java:88)
at org.apache.http.impl.execchain.RedirectExec.execute(RedirectExec.java:110)
at org.apache.http.impl.client.InternalHttpClient.doExecute(InternalHttpClient.java:184)
at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:82)
at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:107)
at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:55)
at com.arronlong.httpclientutil.HttpClientUtil.execute(HttpClientUtil.java:422)
... 3 more

Entity无法使用

使用EntityUtils转换返回的entity时报错,显示使用了已经被关闭的流

pom.xml

这里面的引入4.4.1版本之后的包的报错了,只有版本4.5.4没有报错。

https报错

java.security.cert.CertificateParsingException: X.509 Certificate is incomplete: subject field is empty, and SubjectAlternativeName extension is absent
用https会报这个错

多线程问题

目前版本多线程会出现Bug,由于HttpConfig造成的。

第一次访问TLSv1.3的地址会报错,目前发现是打开了线程池的原因

使用作者的demo复现的bug,下载代码后找到test下面的Demo类,注释掉
// //最简单的使用: // String html = HttpClientUtil.get(HttpConfig.custom().url(url).client(HCB.custom().sslpv(SSLProtocolVersion.TLSv1_2).ssl().build())); // System.out.println(html);

然后运行就会报错
Exception in thread "main" com.arronlong.httpclientutil.exception.HttpProcessException: javax.net.ssl.SSLException: Received fatal alert: protocol_version at com.arronlong.httpclientutil.HttpClientUtil.execute(HttpClientUtil.java:462) at com.arronlong.httpclientutil.HttpClientUtil.send(HttpClientUtil.java:377) at com.arronlong.httpclientutil.HttpClientUtil.get(HttpClientUtil.java:102) at com.arronlong.httpclientutil.test.Demo.main(Demo.java:77)

建议作者修复这个bug前,谨慎使用线程池功能

环境为jdk1.8

代码里没有关于TLSv1.3的枚举成语,
SSLProtocolVersion
SSL("SSL"), SSLv3("SSLv3"), TLSv1("TLSv1"), TLSv1_1("TLSv1.1"), TLSv1_2("TLSv1.2"),

关于map传参

HttpConfig config =HttpConfig.custom().url(url)
.map(map)
.method(HttpMethods.GET);
map传参为什么后端接收不到参数。

发送POST请求携带中文参数

发送POST请求携带中文参数会报400错误.
接口接受的是String类型

请求地址:
https://xxxxx/hystrix/user
请求方式:
POST
JSON串:
{
"name":"aaa",
"age":180
}
响应结果:
{"name":"aaa","age":180}

请求地址:
https://xxxxx/hystrix/user
请求方式:
POST
JSON串:
{
"name":"测试",
"age":180
}
响应结果:
{"timestamp":"2020-08-04T04:31:52.593+0000","path":"/hystrix/user","status":400,"error":"Bad Request","message":"Failed to read HTTP message"}

302重定向报错

向一个网址发送post请求后,然后网址返回302重定向,我想获取重定向的locations,获取到的是:不支持此消息类型,看不到我想要的东西,这一点怎么解决?

一些建议想要交流

超时时间

问题

目前设置超时时间都是HCB.custom().timeout(xxx)的方式进行设置,如果是连接池的话,其设置的是该池中所有Http请求的超时时间。而如果我们想要以更低粒度的方式设置超时时间,也就是说为每个连接池的每个Http请求设置不同的超时时间,目前没有办法设置。

解决方案

  1. 添加HttpConfig对象设置超时时间的方法,后面在通过HttpRequestBase对象设置RequestConfig的方式进行设置。
  2. 为HttpConfig添加默认的RequestConfig对象,添加设置该对象的方法,这样粒度更大,用户想要自定义时能够更好的扩展。

执行请求,返回结果

问题

在HttpClientUtil执行Http请求的方法中,方法的粒度太小了,比如我想要Http响应头和Http body的话就没有办法同时拿到。

解决方案

  1. 添加粒度更大的接口进行处理Http请求,可以需要进行把HttpClientUtil执行请求的方法进行重构以便能够更好的使用。

https 连续请求会出错

单个case运行没问题,连续运行的情况最后一个post请求的map 数据不全,这是为什么
比如
Map<String,Object> formdata=new HashMap<String, Object>(); formdata.put("pageNo","0"); formdata.put("pageSize","20");

但是看请求的info 是只有一个键值对,我的几个case用的是同一个client

log4j报错

怎么关闭你项目里面那个log4j呢 java.io.FileNotFoundException: /logs/httpclient/httputil.log (No such file or directory)

关于传参的一个问题

我用这个调用微信接口。有些接口是推xml数据,不需要key,请问有没有设置的方法
因为我看现在好像只能是推key-value的map

请求 httpsssl报错

Exception in thread "main" com.arronlong.httpclientutil.exception.HttpProcessException: javax.net.ssl.SSLException: Received fatal alert: protocol_version
at com.arronlong.httpclientutil.HttpClientUtil.execute(HttpClientUtil.java:462)
at com.arronlong.httpclientutil.HttpClientUtil.send(HttpClientUtil.java:377)
at com.arronlong.httpclientutil.HttpClientUtil.post(HttpClientUtil.java:128)
at com.github.binarywang.demo.wx.mp.utils.HttpRequestUtilsTest.main(HttpRequestUtilsTest.java:60)
Caused by: javax.net.ssl.SSLException: Received fatal alert: protocol_version

//最简单的使用:
HCB hcb = HCB.custom()
//.timeout(1000) //超时
.pool(100, 10) //启用连接池,每个路由最大创建10个链接,总连接数限制为100个
.sslpv("TLSv1.2") //可设置ssl版本号,默认SSLv3,用于ssl,也可以调用sslpv("TLSv1.2")
.ssl() //https,支持自定义ssl证书路径和密码,ssl(String keyStorePath, String keyStorepass)
.retry(5) //重试5次
;

    HttpClient client = hcb.build();

    Map<String, Object> map = new HashMap<String, Object>();
    map.put("key1", "value1");
    map.put("key2", "value2");

    //插件式配置请求参数(网址、请求参数、编码、client)
    HttpConfig config = HttpConfig.custom()

        .timeout(1000) 		//超时
        .url(url)           //设置请求的url

// .map(map) //设置请求参数,没有则无需设置
.encoding("utf-8") //设置请求和返回编码,默认就是Charset.defaultCharset()
.client(client) //如果只是简单使用,无需设置,会自动获取默认的一个client对象
//.inenc("utf-8") //设置请求编码,如果请求返回一直,不需要再单独设置
//.inenc("utf-8") //设置返回编码,如果请求返回一直,不需要再单独设置
.json("{\n" +
" "current_openid": "199",\n" +
" "gzhid": "1"\n" +
"\n" +
"}") //json方式请求的话,就不用设置map方法,当然二者可以共用。
//.context(HttpCookies.custom().getContext()) //设置cookie,用于完成携带cookie的操作
//.out(new FileOutputStream("保存地址")) //下载的话,设置这个方法,否则不要设置
//.files(new String[]{"d:/1.txt","d:/2.txt"}) //上传的话,传递文件路径,一般还需map配置,设置服务器保存路径
;

    //使用方式:

    String result2 = HttpClientUtil.post(config);   //post请求

    System.out.println(result2);

建议增加双向验证

目前只支持客户端信任服务器证书,如果访问时需要携带客户端证书 这种情况没有加载证书的重载方法 建议增加

body如何设置

看了lz的所有使用说明没有发现设置body的地方啊

关于cookies

如果我本地已经获取到了cookies,如何根据cookies来访问登陆的网站。
HttpConfig里面的context和header都设置过了,返回的仍然是没有登陆过的页面

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.