httpclient发送get请求Headers传参数
时间: 2023-09-27 17:10:02 浏览: 183
使用Apache HttpClient发送GET请求时,Headers可以通过设置请求对象的addHeader方法来传递参数。下面是示例代码:
```java
import java.io.IOException;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
public class HttpClientGetHeadersExample {
public static void main(String[] args) throws IOException {
CloseableHttpClient client = HttpClients.createDefault();
HttpGet httpGet = new HttpGet("https://www.example.com");
httpGet.addHeader("header1", "value1");
httpGet.addHeader("header2", "value2");
CloseableHttpResponse response = client.execute(httpGet);
// 处理响应结果
response.close();
client.close();
}
}
```
在这个例子中,我们创建了一个HttpGet对象,并通过addHeader方法设置了两个header参数。使用CloseableHttpClient执行请求后,可以处理响应结果。
阅读全文