Apache HttpClient 和自定义端口

编程入门 行业动态 更新时间:2024-10-28 21:24:04
本文介绍了Apache HttpClient 和自定义端口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时送ChatGPT账号..

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

I'm using the Apache HttpClient 4 and it works fine. The only thing that doesn't work is custom ports. It seems like the root directory is fetched and the port is ignored.

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

如果没有定义端口,http- 和 https-connections 运行良好.方案注册表定义如下:

If no port is defined, http- and https-connections work well. The scheme registry is defined as follows:

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);

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

How can I define custom ports for a request?

推荐答案

问题是服务器不理解 HTTP 1.1 分块传输.我使用 ByteArrayEntity 缓存了数据,一切正常.

The problem was that the server does not understand HTTP 1.1 chunked transfers. I cached the data by using a ByteArrayEntity and all was ok.

因此自定义端口确实适用于上述代码.

So custom ports do work with the code mentioned above.

这篇关于Apache HttpClient 和自定义端口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

更多推荐

[db:关键词]

本文发布于:2023-04-19 14:53:32,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/963784.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:自定义   端口   Apache   HttpClient

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!