乐闻世界logo
搜索文章和话题

What port does httpclient use?

1个答案

1

When using HTTPClient for network communication, the port it uses typically depends on the protocol employed:

  1. HTTP Protocol: The default port is 80. When your HTTPClient sends requests via HTTP, it automatically uses port 80 unless specified otherwise.

  2. 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:

java
CloseableHttpClient 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.

2024年8月5日 10:09 回复

你的答案