使用 CloseableHttpClient 如何获取 host
时间: 2023-10-14 14:31:01 浏览: 38
要获取 host,可以通过以下步骤:
1. 创建一个 HttpHost 对象,传入主机名和端口号:
```
HttpHost host = new HttpHost("example.com", 80);
```
2. 使用 HttpClientBuilder 创建 CloseableHttpClient 对象,并设置 HttpHost 对象:
```
CloseableHttpClient client = HttpClientBuilder.create().setRoutePlanner(new DefaultProxyRoutePlanner(host)).build();
```
3. 现在你可以使用 CloseableHttpClient 对象来发送请求了。
相关问题
CloseableHttpClient 如何设置参数
CloseableHttpClient 是 Apache HttpComponents 客户端库中的一个类,可以用于向 HTTP 服务器发起请求。它可以通过设置 HttpClientBuilder 对象的参数来配置。以下是一些常见的参数设置方式:
1. 设置连接超时时间(单位:毫秒):
```
int timeout = 5000;
RequestConfig config = RequestConfig.custom()
.setConnectTimeout(timeout)
.setConnectionRequestTimeout(timeout)
.setSocketTimeout(timeout)
.build();
CloseableHttpClient httpClient = HttpClientBuilder.create()
.setDefaultRequestConfig(config)
.build();
```
2. 设置代理服务器:
```
HttpHost proxy = new HttpHost("proxy.example.com", 8080, "http");
RequestConfig config = RequestConfig.custom()
.setProxy(proxy)
.build();
CloseableHttpClient httpClient = HttpClientBuilder.create()
.setDefaultRequestConfig(config)
.build();
```
3. 添加请求头:
```
CloseableHttpClient httpClient = HttpClientBuilder.create()
.setDefaultHeaders(Arrays.asList(
new BasicHeader("Accept-Encoding", "gzip, deflate"),
new BasicHeader("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36")))
.build();
```
4. 开启重定向:
```
CloseableHttpClient httpClient = HttpClientBuilder.create()
.setRedirectStrategy(new LaxRedirectStrategy())
.build();
```
以上是一些常见的参数设置方式,你可以根据自己的需求进行设置。
JAVA 使用 HttpGet 调用生成动态ip代理
在 Java 中使用 `HttpGet` 调用生成动态 IP 代理可以使用 Apache HttpClient 库实现。以下是一个简单的示例代码:
```java
import java.io.IOException;
import org.apache.http.HttpHost;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.utils.HttpClientUtils;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.impl.conn.DefaultProxyRoutePlanner;
public class DynamicProxyExample {
public static void main(String[] args) throws Exception {
// 设置代理 IP 和端口号
String proxyAddress = "127.0.0.1";
int proxyPort = 1080;
// 创建 HttpClient 对象,并设置代理
HttpHost proxy = new HttpHost(proxyAddress, proxyPort);
DefaultProxyRoutePlanner routePlanner = new DefaultProxyRoutePlanner(proxy);
CloseableHttpClient httpClient = HttpClientBuilder.create().setRoutePlanner(routePlanner).build();
// 创建 HttpGet 对象
HttpGet httpGet = new HttpGet("http://www.example.com");
// 发送 HTTP 请求并读取响应
try {
httpClient.execute(httpGet);
// 输出响应内容
System.out.println(httpGet.toString());
} catch (IOException e) {
e.printStackTrace();
} finally {
// 关闭 HttpClient
HttpClientUtils.closeQuietly(httpClient);
}
}
}
```
在上面的示例代码中,我们使用 `HttpHost` 创建代理对象,然后使用 `DefaultProxyRoutePlanner` 创建代理路由计划器,并将其传递给 `HttpClientBuilder` 的 `setRoutePlanner` 方法中。在创建 `HttpGet` 对象时,将代理对象传递给 `HttpGet` 构造函数即可。接下来,发送 HTTP 请求并读取响应。最后,关闭 `HttpClient` 对象。