gpt4 book ai didi

java - Apache HttpClient 和自定义端口

转载 作者:塔克拉玛干 更新时间:2023-11-01 22:00:42 24 4
gpt4 key购买 nike

我使用的是 Apache HttpClient 4,它运行良好。唯一不起作用的是自定义端口。似乎获取了根目录并忽略了端口。

HttpPost post = new HttpPost("http://myserver.com:50000");
HttpResponse response = httpClient.execute(post);

如果没有定义端口,http- 和 https- 连接工作正常。方案注册表定义如下:

final SchemeRegistry sr = new SchemeRegistry();

final Scheme http = new Scheme("http", 80,
PlainSocketFactory.getSocketFactory());
sr.register(http);

final SSLContext sc = SSLContext.getInstance(SSLSocketFactory.TLS);
sc.init(null, TRUST_MANAGER, new SecureRandom());
SSLContext.setDefault(sc);

final SSLSocketFactory sf = new SSLSocketFactory(sc,
SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);

final Scheme https = new Scheme("https", 443, sf);
sr.register(https);

如何为请求定义自定义端口?

最佳答案

一个建议是尝试使用 HttpPost(URI address) 而不是带有 String 参数的那个。您可以显式设置端口:

URI address = new URI("http", null, "my.domain.com", 50000, "/my_file", "id=10", "anchor") 
HttpPost post = new HttpPost(address);
HttpResponse response = httpClient.execute(post);

不能保证这会奏效,但请尝试一下。

关于java - Apache HttpClient 和自定义端口,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7712505/

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