When using HTTPClient for network communication, the port it uses typically depends on the protocol employed:
-
HTTP Protocol: The default port is
80. When your HTTPClient sends requests via HTTP, it automatically uses port 80 unless specified otherwise. -
HTTPS Protocol: The default port is
443. For encrypted HTTPS communication, HTTPClient defaults to using port 443.
Example
For example, if you use Apache HttpClient in Java to access a website, the code might look like:
javaCloseableHttpClient httpclient = HttpClients.createDefault(); HttpGet httpGet = new HttpGet("http://www.example.com"); CloseableHttpResponse response = httpclient.execute(httpGet);
In this example, since the URL uses the http protocol, the HttpGet object will default to accessing www.example.com via port 80. If the URL is https://www.example.com, it will default to using port 443.
In practical applications, the port can be explicitly specified, such as http://www.example.com:8080, at which point the HTTPClient will use port 8080 to send the request.
In summary, the port used by HTTPClient primarily depends on the protocol type and whether a specific port is designated. In most cases, port 80 is used for HTTP, and port 443 for HTTPS.