简体   繁体   English

如何获取java.net.HttpURLConnection使用的源端口?

[英]How to get the source port being used by java.net.HttpURLConnection?

I am using the java.net.HttpURlConnection or javax.net.ssl.HttpsURLConnection objects for connecting to http/https servers. 我正在使用java.net.HttpURlConnectionjavax.net.ssl.HttpsURLConnection对象连接到http / https服务器。 My tests failed at some point because apparently all they had used up all the available ports on my local machine. 我的测试在某个时候失败了,因为显然所有测试都用完了本地计算机上的所有可用端口。 These are persistent http connections and therefore should be reused. 这些是持久的http连接,因此应该重用。

I am trying a few things to fix this. 我正在尝试一些方法来解决此问题。 But to check whether my fix works, I need to make sure that the same port on the source machine is getting used in subsequent instances of HttpURlConnection or HttpsURLConnection . 但是要检查我的修复程序是否有效,我需要确保在后续的HttpURlConnectionHttpsURLConnection实例中使用源计算机上的同一端口。 How can I get the source port being used by java.net.HttpURLConnection ? 如何获取java.net.HttpURLConnection使用的源端口?

Either you specify the port as a part of URL you supply to your HTTP connection or defaultone is used. 您可以将端口指定为提供给HTTP连接的URL的一部分,或者使用defaultone。

Either way, your URL object has this information and you can inquire about it with its getPort() method. 无论哪种方式, 您的 URL对象都有此信息,您可以使用其getPort()方法进行查询。

I don't see any methods that allows same local port bind in HttpURlConnection or get local port number being used for binding. 我没有在HttpURlConnection中看到任何允许相同本地端口绑定的方法,也没有获得用于绑定的本地端口号。 There is some methods in Java's Socket class that allows you to bind same local port ( constructor method and bind() I think ) though... Check PlainConnectionSocketFactory in Apache httpclient 4.x than Socket if you want to go this way since those HttpClient has more handy methods. Java的Socket类中有一些方法可以允许您绑定相同的本地端口(我认为是构造函数方法和bind())...如果您想使用这种方法,请检查Apache httpclient 4.x中的PlainConnectionSocketFactory而不是Socket,因为那些HttpClient有更多方便的方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 处理 GET 请求 - Apache HttpClient 与 java.net.HttpURLConnection - Handling GET requests - Apache HttpClient vs java.net.HttpURLConnection java.net.httpurlconnection 不起作用 - java.net.httpurlconnection just wont work 强制 java.net.HttpUrlConnection 返回 GET 响应,而不考虑 Http-Status-Code - Force java.net.HttpUrlConnection to return GET-response regardless of Http-Status-Code 如何通过java.net.HttpURLConnection发送Post Data或Form数据以访问Web服务 - How to send Post Data or Form data through java.net.HttpURLConnection to access the webservices 多线程Java应用程序中java.net.HttpURLConnection的不同实例 - Different instance of java.net.HttpURLConnection in multithreaded java application 使用java.net.HttpURLConnection时是否可以转储HTTP标头? - Is it possible to dump the HTTP Headers when using java.net.HttpURLConnection? 为什么java.net.HttpURLConnection响应不明字符? - why java.net.HttpURLConnection responsed unidentified characters? 处理与服务器不存在的java.net.HttpURLConnection - Handling java.net.HttpURLConnection to server that does not exist java.net.HttpURLConnection的默认超时记录在哪里? - Where are the java.net.HttpURLConnection default timeouts documented? 需要有关怪异的java.net.HttpURLConnection行为的帮助 - need help with bizarre java.net.HttpURLConnection behavior
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM