HttpClient
了解get和post区别,参考这里
无非是代码模拟人工点击浏览器
3个步骤
-
打开浏览器
CloseableHttpClient httpClient = HttpClients.createDefault();
-
输入网址
HttpGet httpGet = new HttpGet("网址....");
-
回车,返回界面
CloseableHttpResponse response = httpClient.execute(httpGet);
Get无参
查看百度首页内容
public class HttpTest{
public static void main(String[] args) {
//模拟打开浏览器
CloseableHttpClient httpClient = HttpClients.createDefault();
//创建GET请求,设置URL访问地址
HttpGet httpGet = new HttpGet("http://www.baidu.com");
CloseableHttpResponse response=null;
try {
//使用HttpClient发起请求,获取response
response = httpClient.execute(httpGet);
//解析响应
if(response!=null){
//输出getStatusLine()
// 成功 HTTP/1.1 200 OK
// 失败 HTTP/1.1 403 Forbidden
//获取状态码,如 200 或者403
System.out.println(response.getStatusLine().getStatusCode());
HttpEntity entity = response.getEntity();//实体
String content = EntityUtils.toString(entity, "utf8");//把实体转为string
System.out.println(content);
}
} catch (IOException e) {
e.printStackTrace();
}finally {
//关闭,节省资源
try {
response.close();
} catch (IOException e) {
e.printStackTrace();
}finally {
try {
httpClient.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
Get有参
输入网址也有参数啊,不能光
http://www.baidu.com 啊
比如访问百度百科,这是有参数的
https://baike.baidu.com/item/0/11071655?fr=aladdin
由于httpGet的参数也能是URI,而URI的创建需要URIbuilder,所以
public class HttpTest{
public static void main(String[] args) throws URISyntaxException {
//模拟打开浏览器
CloseableHttpClient httpClient = HttpClients.createDefault();
//构造带参数的地址
//https://baike.baidu.com/item/0/11071655?fr=aladdin
URIBuilder uriBuilder = new URIBuilder("https://baike.baidu.com/item/0/11071655");
uriBuilder.setParameter("fr","aladdin");//参数
//创建GET请求,设置URI访问地址
HttpGet httpGet = new HttpGet(uriBuilder.build());
CloseableHttpResponse response=null;
try {
//使用HttpClient发起请求,获取response
response = httpClient.execute(httpGet);
//解析响应
if(response!=null){
System.out.println(response.getStatusLine());
HttpEntity entity = response.getEntity();
String content = EntityUtils.toString(entity, "utf8");
System.out.println(content);
}
} catch (IOException e) {
e.printStackTrace();
}finally {
//关闭response
try {
response.close();
} catch (IOException e) {
e.printStackTrace();
}finally {
try {
httpClient.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
无参post
和无参get差不多,只需改变一行
HttpGet httpGet = new HttpGet("网址....");
改为
HttpPost httpPost = new HttpPost("网址....");
带参post
public class HttpTest{
public static void main(String[] args) throws URISyntaxException, UnsupportedEncodingException {
//模拟打开浏览器
CloseableHttpClient httpClient = HttpClients.createDefault();
//https://baike.baidu.com/item/0/11071655?fr=aladdin
HttpPost httpPost = new HttpPost("https://baike.baidu.com/item/0/11071655");
//声明list集合,封装表单的中的参数
ArrayList<NameValuePair> params = new ArrayList<>();
params.add(new BasicNameValuePair("fr","aladdin"));
//创建表单的Entity对象,参数为表单数据和编码
UrlEncodedFormEntity formEntity = new UrlEncodedFormEntity(params, "utf8");
//将表单Entity对象加入到post请求中
httpPost.setEntity(formEntity);
CloseableHttpResponse response=null;
try {
//使用HttpClient发起请求,获取response
response = httpClient.execute(httpPost);
//解析响应
if(response!=null){
System.out.println(response.getStatusLine());
HttpEntity entity = response.getEntity();
String content = EntityUtils.toString(entity, "utf8");
System.out.println(content);
}
} catch (IOException e) {
e.printStackTrace();
}finally {
//关闭response
try {
response.close();
} catch (IOException e) {
e.printStackTrace();
}finally {
try {
httpClient.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
));