带参数的get请求
//创建httpclient对象
CloseableHttpClient httpClient = HttpClients.createDefault();
//设置请求地址
//创建urlBuilder
URIBuilder uriBuilder=new URIBuilder("http://yun.itheima.com/search");
//设置参数
uriBuilder.setParameter("keys","Java");
// uriBuilder.setParameter("keys","Java").setParameter(); 参数可以继续往下设置
//创建httpget对象 设置url访问地址
HttpGet httpGet=new HttpGet(uriBuilder.build());
System.out.println(httpGet);
//使用httpclient发起请求 获取response
CloseableHttpResponse response = null;
try {
response = httpClient.execute(httpGet);
if (response.getStatusLine().getStatusCode()==200){
String content = null;
content = EntityUtils.toString(response.getEntity(), "utf8");
System.out.println(content.length());
}
} catch (Exception e) {
e.printStackTrace();
}finally {
try {
response.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
httpClient.close();
} catch (IOException e) {
e.printStackTrace();
}
}