@HttpClient的学习笔记
一、HttpClient的请求类型
HttpGet、HttpPost、HttpPut、HttpDelete四种请求类型
二、使用流程
a、HttpGet请求
//1.打开浏览器
CloseableHttpClient httpClient = HttpClients.createDefault();
//2.声明get请求
HttpGet httpGet = new HttpGet("http://www.baidu.com/s?wd=java");
//3.发送请求
CloseableHttpResponse response = httpClient.execute(httpGet);
//4.判断状态码
if(response.getStatusLine().getStatusCode()==200){
HttpEntity entity = response.getEntity();
//使用工具类EntityUtils,从响应中取出实体表示的内容并转换成字符串
String string = EntityUtils.toString(entity, "utf-8");
System.out.println(string);
}
//5.关闭资源
response.close();
httpClient.close();
b、HttpPost请求
//1.打开浏览器
CloseableHttpClient httpClient = HttpClients.createDefault();
//2.声明get请求
HttpPost httpPost = new HttpPost("https://www.oschina.net/");
//3.开源中国为了安全,防止恶意攻击,在post请求中都限制了浏览器才能访问
httpPost.addHeader("User-Agent","Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.106 Safari/537.36");
//4.判断状态码
List<NameValuePair> parameters = new ArrayList<NameValuePair>(0);
parameters.add(new BasicNameValuePair("scope", "project"));
parameters.add(new BasicNameValuePair("q", "java"));
UrlEncodedFormEntity formEntity = new UrlEncodedFormEntity(parameters,"UTF-8");
httpPost.setEntity(formEntity);
//5.发送请求
CloseableHttpResponse response = httpClient.execute(httpPost);
if(response.getStatusLine().getStatusCode()==200){
HttpEntity entity = response.getEntity();
String string = EntityUtils.toString(entity, "utf-8");
System.out.println(string);
}
//6.关闭资源
response.close();
httpClient.close();