gpt4 book ai didi

java - HttpClient ThreadSafeClientConnManager 抛出 "Connection reset"

转载 作者:行者123 更新时间:2023-11-30 05:15:52 24 4
gpt4 key购买 nike

我用两种不同的方式定义 HttpClient:1. 普通: client = new DefaultHttpClient();2.线程安全:

DefaultHttpClient getThreadSafeHttpClient() {
HttpParams params = new BasicHttpParams();
params
.setParameter(
"http.useragent",
"Mozilla/5.0 (Linux; U; Android 1.1; en-us;dream) AppleWebKit/525.10+ (KHTML, like Gecko) Version/3.0.4 Mobile Safari/523.12.2");
HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);
HttpProtocolParams.setContentCharset(params, "UTF-8");
final SchemeRegistry registry = new SchemeRegistry();
registry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));
registry.register(new Scheme("https", PlainSocketFactory.getSocketFactory(), 443));
final ThreadSafeClientConnManager manager = new ThreadSafeClientConnManager(params,
registry);
return new DefaultHttpClient(manager, params);
}

然后我对两种客户端类型运行相同的 JUnit 测试(简单的 GET 请求)。 #1 总是运行良好,#2 总是失败并显示“java.net.SocketException:连接重置”。调试/堆栈跟踪输出可以是 seen here (虚构网站)

我从来没有机会对实体对象执行任何操作,因为在 client#execute 调用时抛出错误。我在这里做错了什么?

最佳答案

啊哈!原因是使用错误的套接字工厂定义 HTTPS。应该是SSLSocketFactory

SSLSocketFactory sslSocketFactory = SSLSocketFactory.getSocketFactory();
sslSocketFactory.setHostnameVerifier(SSLSocketFactory.STRICT_HOSTNAME_VERIFIER);
registry.register(new Scheme("https", sslSocketFactory, 443));

关于java - HttpClient ThreadSafeClientConnManager 抛出 "Connection reset",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1342681/

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