HttpClient get请求传参实例
时间: 2023-05-22 12:06:30 浏览: 193
好的,以下是一个HttpClient的get请求传参的示例代码:
```java
CloseableHttpClient httpclient = HttpClients.createDefault();
HttpGet httpGet = new HttpGet("http://www.example.com/search");
httpGet.addHeader("Content-Type", "application/json");
List<NameValuePair> params = new ArrayList<>();
params.add(new BasicNameValuePair("q", "httpclient"));
params.add(new BasicNameValuePair("btnG", "Google Search"));
params.add(new BasicNameValuePair("aq", "f"));
params.add(new BasicNameValuePair("oq", ""));
HttpResponse response = null;
try {
URI uri = new URIBuilder(httpGet.getURI())
.addParameters(params)
.build();
httpGet.setURI(uri);
response = httpclient.execute(httpGet);
} catch (Exception e) {
e.printStackTrace();
}
```
这段代码使用了HttpClients库中的CloseableHttpClient和HttpGet类,通过添加BasicNameValuePair参数的方式,将传参信息添加到url中来发送get请求,并将服务器返回的HttpResponse对象进行处理。
阅读全文