gpt4 book ai didi

java - Apache HttpClient 执行方法没有主机端口号

转载 作者:行者123 更新时间:2023-12-01 19:50:04 24 4
gpt4 key购买 nike

我们正在 Java 中创建一个反向代理应用程序,它仅将请求转发到下游系统并获取响应并将其发送回 UI。

为此,我们开始使用 Apache HttpClient 库。当我们像下面这样配置时,Http 客户端执行就可以完美地工作。它正在处理所有请求(GET、POST、PUT、DELETE)。它是以通用方式制作的。

 HttpRequest request = null;
if (request.getHeader(HttpHeaders.CONTENT_LENGTH) != null
|| request.getHeader(HttpHeaders.TRANSFER_ENCODING) != null) {
HttpEntityEnclosingRequest proxyRequest = new BasicHttpEntityEnclosingRequest(method, requestUri);
proxyRequest.setEntity(new InputStreamEntity(servletRequest.getInputStream(), getContentLength(servletRequest)));

} else {
proxyRequest = new BasicHttpRequest(method, requestUri);
}
request = proxyRequest;
HttpResponse response = httpClient.execute(new HttpHost("http://custdomain.com", 8443, "https"),request);

但是当我尝试仅使用不带端口号的网址时,我无法连接到下游系统。

HttpResponse response = httpClient.execute(new HttpHost("http://custdomain.com",-1, "https"),request);

请提出建议。

Edit1:连接到 8443 端口时出现连接重置异常

2018-08-03 17:22:28.961 DEBUG 7920 --- [nio-9003-exec-1] org.apache.http.headers                  : http-outgoing-0 >> CONNECT quotesystem.com:8443 HTTP/1.1
2018-08-03 17:22:28.966 DEBUG 7920 --- [nio-9003-exec-1] org.apache.http.headers : http-outgoing-0 >> Host: quotesystem.com:8443
2018-08-03 17:22:28.971 DEBUG 7920 --- [nio-9003-exec-1] org.apache.http.headers : http-outgoing-0 >> User-Agent: Apache-HttpClient/4.5.3 (Java/1.8.0_131)
2018-08-03 17:22:28.976 DEBUG 7920 --- [nio-9003-exec-1] org.apache.http.wire : http-outgoing-0 >> "CONNECT quotesystem.com:8443 HTTP/1.1[\r][\n]"
2018-08-03 17:22:28.981 DEBUG 7920 --- [nio-9003-exec-1] org.apache.http.wire : http-outgoing-0 >> "Host: quotesystem.com:8443[\r][\n]"
2018-08-03 17:22:28.986 DEBUG 7920 --- [nio-9003-exec-1] org.apache.http.wire : http-outgoing-0 >> "User-Agent: Apache-HttpClient/4.5.3 (Java/1.8.0_131)[\r][\n]"
2018-08-03 17:22:28.988 DEBUG 7920 --- [nio-9003-exec-1] org.apache.http.wire : http-outgoing-0 >> "[\r][\n]"
2018-08-03 17:22:29.018 DEBUG 7920 --- [nio-9003-exec-1] org.apache.http.wire : http-outgoing-0 << "HTTP/1.1 200 Connection Established[\r][\n]"
2018-08-03 17:22:29.023 DEBUG 7920 --- [nio-9003-exec-1] org.apache.http.wire : http-outgoing-0 << "Proxy-Agent: Zscaler/6.0[\r][\n]"
2018-08-03 17:22:29.026 DEBUG 7920 --- [nio-9003-exec-1] org.apache.http.wire : http-outgoing-0 << "[\r][\n]"
2018-08-03 17:22:29.081 DEBUG 7920 --- [nio-9003-exec-1] org.apache.http.headers : http-outgoing-0 << HTTP/1.1 200 Connection Established
2018-08-03 17:22:29.086 DEBUG 7920 --- [nio-9003-exec-1] org.apache.http.headers : http-outgoing-0 << Proxy-Agent: Zscaler/6.0
2018-08-03 17:22:29.123 DEBUG 7920 --- [nio-9003-exec-1] o.a.http.impl.execchain.MainClientExec : Tunnel to target created.
2018-08-03 17:22:35.686 DEBUG 7920 --- [nio-9003-exec-1] o.a.h.c.ssl.SSLConnectionSocketFactory : Enabled protocols: [TLSv1, TLSv1.1, TLSv1.2]
2018-08-03 17:22:35.689 DEBUG 7920 --- [nio-9003-exec-1] o.a.h.c.ssl.SSLConnectionSocketFactory : Enabled cipher suites:[TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA256]
2018-08-03 17:22:35.694 DEBUG 7920 --- [nio-9003-exec-1] o.a.h.c.ssl.SSLConnectionSocketFactory : Starting handshake
2018-08-03 17:23:11.553 DEBUG 7920 --- [nio-9003-exec-1] h.i.c.DefaultManagedHttpClientConnection : http-outgoing-0: Shutdown connection
2018-08-03 17:23:11.569 DEBUG 7920 --- [nio-9003-exec-1] o.a.http.impl.execchain.MainClientExec : Connection discarded
2018-08-03 17:23:11.584 DEBUG 7920 --- [nio-9003-exec-1] h.i.c.PoolingHttpClientConnectionManager : Connection released: [id: 0][route: {tls}->http://127.0.0.1:9874->https://quotesystem.com:8443][total kept alive: 0; route allocated: 0 of 2; total allocated: 0 of 100]
2018-08-03 17:23:11.619 INFO 7920 --- [nio-9003-exec-1] o.apache.http.impl.execchain.RetryExec : I/O exception (java.net.SocketException) caught when processing request to {tls}->http://127.0.0.1:9874->https://quotesystem.com:8443: Connection reset
2018-08-03 17:23:11.849 DEBUG 7920 --- [nio-9003-exec-1] o.apache.http.impl.execchain.RetryExec : Connection reset


java.net.SocketException: Connection reset
at java.net.SocketInputStream.read(SocketInputStream.java:210) ~[na:1.8.0_131]
at java.net.SocketInputStream.read(SocketInputStream.java:141) ~[na:1.8.0_131]
at sun.security.ssl.InputRecord.readFully(InputRecord.java:465) ~[na:1.8.0_131]
at sun.security.ssl.InputRecord.read(InputRecord.java:503) ~[na:1.8.0_131]
at sun.security.ssl.SSLSocketImpl.readRecord(SSLSocketImpl.java:973) ~[na:1.8.0_131]
at sun.security.ssl.SSLSocketImpl.performInitialHandshake(SSLSocketImpl.java:1375) ~[na:1.8.0_131]
at sun.security.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:1403) ~[na:1.8.0_131]
at sun.security.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:1387) ~[na:1.8.0_131]
at org.apache.http.conn.ssl.SSLConnectionSocketFactory.createLayeredSocket(SSLConnectionSocketFactory.java:396) ~[httpclient-4.5.3.jar:4.5.3]
at org.apache.http.impl.conn.DefaultHttpClientConnectionOperator.upgrade(DefaultHttpClientConnectionOperator.java:193) ~[httpclient-4.5.3.jar:4.5.3]
at org.apache.http.impl.conn.PoolingHttpClientConnectionManager.upgrade(PoolingHttpClientConnectionManager.java:375) ~[httpclient-4.5.3.jar:4.5.3]
at org.apache.http.impl.execchain.MainClientExec.establishRoute(MainClientExec.java:416) ~[httpclient-4.5.3.jar:4.5.3]
at org.apache.http.impl.execchain.MainClientExec.execute(MainClientExec.java:237) ~[httpclient-4.5.3.jar:4.5.3]
at org.apache.http.impl.execchain.ProtocolExec.execute(ProtocolExec.java:185) ~[httpclient-4.5.3.jar:4.5.3]
at org.apache.http.impl.execchain.RetryExec.execute(RetryExec.java:89) ~[httpclient-4.5.3.jar:4.5.3]
at org.apache.http.impl.execchain.RedirectExec.execute(RedirectExec.java:111) [httpclient-4.5.3.jar:4.5.3]
at org.apache.http.impl.client.InternalHttpClient.doExecute(InternalHttpClient.java:185) [httpclient-4.5.3.jar:4.5.3]
at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:118) [httpclient-4.5.3.jar:4.5.3]
at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:56) [httpclient-4.5.3.jar:4.5.3]

最佳答案

通过启用 TLSv1.2 解决了此问题

关于java - Apache HttpClient 执行方法没有主机端口号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51650852/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com