博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
HttpClient使用教程
阅读量:6042 次
发布时间:2019-06-20

本文共 4209 字,大约阅读时间需要 14 分钟。

hot3.png

1.使用HttpClient发送get请求(不带参数)

import org.apache.http.impl.client.CloseableHttpClient;import org.apache.http.impl.client.HttpClients;import org.apache.http.util.EntityUtils;import org.apache.http.client.methods.HttpGet;import org.apache.http.HttpResponse;import org.apache.http.HttpEntity;// 创建一个HttpClient实例CloseableHttpClient httpClient = null;try{    httpClient = HttpClients.createDefault();    // 创建一个HttpGet实例    HttpGet httpGet = new HttpGet("http://127.0.0.1:8080/test/demo");    // 发送GET请求    HttpResponse execute = httpClient.execute(httpGet);    //获取状态码    int statusCode = execute.getStatusLine().getStatusCode();    System.out.println(statusCode);    HttpEntity entity = execute.getEntity();    String resutlt = EntityUtils.toString(entity, "utf-8");    System.out.println(resutlt);}catch (Exception e) {	e.printStackTrace();}finally{   if (httpClient != null) {	try {		httpClient.close();	} catch (IOException e) {		e.printStackTrace();	}   }}

2.使用HttpClient发送get请求(带参数)

import org.apache.http.impl.client.CloseableHttpClient;import org.apache.http.impl.client.HttpClients;import org.apache.http.message.BasicNameValuePair;import org.apache.http.NameValuePair;import org.apache.http.util.EntityUtils;import org.apache.http.client.entity.UrlEncodedFormEntity;import org.apache.http.Consts;import org.apache.http.client.methods.HttpGet;import org.apache.http.HttpResponse;import org.apache.http.HttpEntity;import java.util.ArrayList;import java.util.List;// 创建一个HttpClient实例CloseableHttpClient httpClient = null;try{    httpClient  = HttpClients.createDefault();    // 装填参数    List
params = new ArrayList
(); params.add(new BasicNameValuePair("id", "1")); //字符串参数 // 转换为键值对 String str = EntityUtils.toString(new UrlEncodedFormEntity(nvps, Consts.UTF_8)); // 创建一个HttpGet实例 HttpGet httpGet = new HttpGet("http://127.0.0.1:8080/test/demo?"+str); // 发送GET请求 HttpResponse execute = httpClient.execute(httpGet); //获取状态码 int statusCode = execute.getStatusLine().getStatusCode(); System.out.println(statusCode); HttpEntity entity = execute.getEntity(); String resutlt = EntityUtils.toString(entity, "utf-8"); System.out.println(resutlt);}catch (Exception e) { e.printStackTrace();} finally { if (httpClient != null) { try { httpClient.close(); } catch (IOException e) { e.printStackTrace(); } }}

3.使用HttpClient发送post请求

import org.apache.http.impl.client.CloseableHttpClient;import org.apache.http.impl.client.HttpClients;import org.apache.http.message.BasicNameValuePair;import org.apache.http.NameValuePair;import org.apache.http.util.EntityUtils;import org.apache.http.client.entity.UrlEncodedFormEntity;import org.apache.http.Consts;import org.apache.http.client.methods.HttpPost;import org.apache.http.HttpResponse;import org.apache.http.HttpEntity;import java.util.ArrayList;import java.util.List;// 创建HttpClient实例CloseableHttpClient httpClient = null;// 创建HttpPost实例HttpPost httpPost = new HttpPost("http://127.0.0.1/test/demo");// 装填参数List
params = new ArrayList
();params.add(new BasicNameValuePair("id", "1")); //字符串参数 可以循环添加多个try { httpClient = HttpClients.createDefault(); // 设置请求参数 httpPost.setEntity(new UrlEncodedFormEntity(params, "utf-8")); // 设置header信息 httpPost.setHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8"); httpPost.setHeader("User-Agent","Mozilla/5.0 (Windows NT 6.1; WOW64; rv:56.0) Gecko/20100101 Firefox/56.0"); // 执行请求 HttpResponse httpResponse = httpClient.execute(httpPost); HttpEntity resEntity = httpResponse.getEntity(); // 按指定编码转换结果实体为String类型 String result = EntityUtils.toString(resEntity, "utf-8"); System.out.println(result);} catch (Exception e) { e.printStackTrace();} finally { if (httpClient != null) { try { httpClient.close(); } catch (IOException e) { e.printStackTrace(); } }}

参考:http://blog.csdn.net/xiaoxian8023/article/details/49863967

maven 下载包

<dependency>

            <groupId>org.apache.httpcomponents</groupId>
            <artifactId>httpclient</artifactId>
            <version>4.5.2</version>
 </dependency>

转载请注明来源:https://my.oschina.net/u/3183495/blog/1574130

转载于:https://my.oschina.net/u/3183495/blog/1574130

你可能感兴趣的文章
2.4 salt grains与pillar jinja的模板
查看>>
VDI序曲二十 桌面虚拟化和RemoteApp集成到SharePoint 2010里
查看>>
移动互联网,入口生死战
查看>>
JAVA多线程深度解析
查看>>
Kafka High Level Consumer 会丢失消息
查看>>
时间轴
查看>>
java 获取系统当前时间的方法
查看>>
Ubuntu 10.04升级git 到1.7.2或更高的可行方法
查看>>
Spring Security4实战与原理分析视频课程( 扩展+自定义)
查看>>
第一周博客作业
查看>>
thinkpython2
查看>>
oracle recyclebin与flashback drop
查看>>
svmlight使用说明
查看>>
Swing 和AWT之间的关系
查看>>
Mysql设置自增长主键的初始值
查看>>
Android计时器正确应用方式解析
查看>>
获取post传输参数
查看>>
ASP生成静态页面的方法
查看>>
HDU 1325 Is It A Tree? 判断是否为一棵树
查看>>
Shell命令-文件压缩解压缩之gzip、zip
查看>>