分享web开发知识

注册/登录|最近发布|今日推荐

主页 IT知识网页技术软件开发前端开发代码编程运营维护技术分享教程案例
当前位置:首页 > IT知识

HttpClient的GET请求(post)请求

发布时间:2023-09-06 02:12责任编辑:蔡小小关键词:GET

一。不带参数的GET请求

// 创建Httpclient对象
???????CloseableHttpClient httpclient = HttpClients.createDefault();

???????// 创建http GET请求
???????HttpGet httpGet = new HttpGet("http://www.baidu.com/");

???????CloseableHttpResponse response = null;
???????try {
???????????// 执行请求
???????????response = httpclient.execute(httpGet);
???????????// 判断返回状态是否为200
???????????if (response.getStatusLine().getStatusCode() == 200) {
???????????????String content = EntityUtils.toString(response.getEntity(), "UTF-8");
???????????????System.out.println("内容长度:"+content.length());
???????????}
???????} finally {
???????????if (response != null) {
???????????????response.close();
???????????}
???????????httpclient.close();
???????}

 

二。带参数的GET请求

// 创建Httpclient对象
???????CloseableHttpClient httpclient = HttpClients.createDefault();

???????// 定义请求的参数
???????URI uri = new URIBuilder("http://www.baidu.com/s").setParameter("wd", "java").build();

???????System.out.println(uri);

???????// 创建http GET请求
???????HttpGet httpGet = new HttpGet(uri);

???????CloseableHttpResponse response = null;
???????try {
???????????// 执行请求
???????????response = httpclient.execute(httpGet);
???????????// 判断返回状态是否为200
???????????if (response.getStatusLine().getStatusCode() == 200) {
???????????????String content = EntityUtils.toString(response.getEntity(), "UTF-8");
???????????????System.out.println(content);
???????????}
???????} finally {
???????????if (response != null) {
???????????????response.close();
???????????}
???????????httpclient.close();
???????}

 

 三。不带参数的POST请求

// 创建Httpclient对象
???????CloseableHttpClient httpclient = HttpClients.createDefault();

???????// 创建http POST请求
???????HttpPost httpPost = new HttpPost("http://www.oschina.net/");
???????//设置请求头信息,可以伪装成浏览器访问
???????httpPost.setHeader("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");

???????CloseableHttpResponse response = null;
???????try {
???????????// 执行请求
???????????response = httpclient.execute(httpPost);
???????????// 判断返回状态是否为200
???????????if (response.getStatusLine().getStatusCode() == 200) {
???????????????String content = EntityUtils.toString(response.getEntity(), "UTF-8");
???????????????System.out.println(content);
???????????}
???????} finally {
???????????if (response != null) {
???????????????response.close();
???????????}
???????????httpclient.close();
???????}

 

 四。带参数的POST请求

// 创建Httpclient对象
???????CloseableHttpClient httpclient = HttpClients.createDefault();

???????// 创建http POST请求
???????HttpPost httpPost = new HttpPost("http://www.oschina.net/search");

???????// 设置2个post参数,一个是scope、一个是q
???????List<NameValuePair> parameters = new ArrayList<NameValuePair>(0);
???????parameters.add(new BasicNameValuePair("scope", "project"));
???????parameters.add(new BasicNameValuePair("q", "java"));
???????// 构造一个form表单式的实体
???????UrlEncodedFormEntity formEntity = new UrlEncodedFormEntity(parameters);
???????// 将请求实体设置到httpPost对象中
???????httpPost.setEntity(formEntity);

???????CloseableHttpResponse response = null;
???????try {
???????????// 执行请求
???????????response = httpclient.execute(httpPost);
???????????// 判断返回状态是否为200
???????????if (response.getStatusLine().getStatusCode() == 200) {
???????????????String content = EntityUtils.toString(response.getEntity(), "UTF-8");
???????????????System.out.println(content);
???????????}
???????} finally {
???????????if (response != null) {
???????????????response.close();
???????????}
???????????httpclient.close();
???????}

 五。如果考虑性能问题,可以跟创建数据库连接池的做法一样,这里创建一个连接池;例子如下:

PoolingHttpClientConnectionManager cm = new PoolingHttpClientConnectionManager();
???????// 设置最大连接数
???????cm.setMaxTotal(200);
???????// 设置每个主机地址的并发数
???????cm.setDefaultMaxPerRoute(20);

doGet(cm);

doGet(cm);

//这里是调用

public static void doGet(HttpClientConnectionManager cm) throws Exception {
???????CloseableHttpClient httpClient = HttpClients.custom().setConnectionManager(cm).build();

???????// 创建http GET请求
???????HttpGet httpGet = new HttpGet("http://www.baidu.com/");

???????CloseableHttpResponse response = null;
???????try {
???????????// 执行请求
???????????response = httpClient.execute(httpGet);
???????????// 判断返回状态是否为200
???????????if (response.getStatusLine().getStatusCode() == 200) {
???????????????String content = EntityUtils.toString(response.getEntity(), "UTF-8");
???????????????System.out.println("内容长度:" + content.length());
???????????}
???????} finally {
???????????if (response != null) {
???????????????response.close();
???????????}
???????????// 此处不能关闭httpClient,如果关闭httpClient,连接池也会销毁
???????????// httpClient.close();
???????}
???}

HttpClient的GET请求(post)请求

原文地址:https://www.cnblogs.com/lingtiaoti/p/9535612.html

知识推荐

我的编程学习网——分享web前端后端开发技术知识。 垃圾信息处理邮箱 tousu563@163.com 网站地图
icp备案号 闽ICP备2023006418号-8 不良信息举报平台 互联网安全管理备案 Copyright 2023 www.wodecom.cn All Rights Reserved