HttpClient get请求传参实例
时间: 2023-05-22 20:06:32 浏览: 163
以下是一个使用HttpClient进行get请求传参的示例:
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URI;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.impl.client.DefaultHttpClient;
public class HttpClientExample {
public static void main(String[] args) {
try {
URI uri = new URIBuilder("http://www.example.com").addParameter("param1", "value1").addParameter("param2", "value2").build();
HttpGet httpget = new HttpGet(uri);
HttpClient httpclient = new DefaultHttpClient();
HttpResponse response = httpclient.execute(httpget);
BufferedReader rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
String line = "";
while ((line = rd.readLine()) != null) {
System.out.println(line);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
这个例子中,我们使用 HttpClient 对 http://www.example.com 发送了一个 GET 请求,同时传递了两个参数:param1 和 param2。使用 URIBuilder 构建 URI 对象,然后将 URI 对象传递给 HttpGet 对象即可。最后获取 HttpResponse 对象并读取响应数据。
阅读全文