apache http get 和 post 请求
1、首先要把jar依赖进项目
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.3</version>
</dependency>
2、get/post方法
/**
* http post 请求
* 1、创建 client 实例
* 2、创建 post 实例
* 3、设置 post 参数
* 4、发送请求
*/
public static String post(String url, Map<String, String> params) {
if (url == null) {
LOGGER.info("http url can not be empty!");
}
String respCtn = "";
// 1、创建 client 实例
CloseableHttpClient httpclient = HttpClients.createDefault();
// 2、创建 post 实例
HttpPost post = new HttpPost(url); ArrayList<NameValuePair> reqParams = null;
if (params != null && !params.isEmpty()) {
reqParams = new ArrayList<NameValuePair>();
for (Map.Entry<String, String> e : params.entrySet()) {
reqParams.add(new BasicNameValuePair(e.getKey(), e.getValue()));
}
} HttpResponse response = null;
try {
if (reqParams != null)
// 3、设置 post 参数
post.setEntity(new UrlEncodedFormEntity(reqParams, "UTF-8"));
// 4、发送请求
response = httpclient.execute(post);
respCtn = EntityUtils.toString(response.getEntity());
} catch (Exception e) {
LOGGER.error("Fail to connect to remote host [" + url + "]" + e);
} finally {
if (httpclient != null) {
try {
httpclient.close();
} catch (IOException e) {
}
}
}
return respCtn;
} /**
* http get 请求
*
* @param String
* url
*
*/
public static String doGet(String url) {
if (url == null || url.isEmpty()) {
LOGGER.info("http url can not be empty!");
}
String respCtn = "";
CloseableHttpClient httpclient = HttpClients.createDefault();
try {
HttpResponse response = null;
HttpGet get = new HttpGet(url);
response = httpclient.execute(get);
respCtn = EntityUtils.toString(response.getEntity());
} catch (Exception e) {
throw new RuntimeException(e.getMessage());
} finally {
if (httpclient != null) {
try {
httpclient.close();
} catch (IOException e) {
}
}
}
return respCtn;
}
apache http get 和 post 请求的更多相关文章
- Apache与Nginx对客户端请求的处理机制对比
Apache与Nginx对客户端请求的处理机制对比 模块 大致为四个模块,核心模块.HTTP模块.邮件模块,以及第三方模块 核心模块主要包含两类功能的支持,一类是主体功能,包括进程管理,权限管理,错误 ...
- JAVA使用apache http组件发送POST请求
在上一篇文章中,使用了JDK中原始的HttpURLConnection向指定URL发送POST请求 可以看到编码量有些大,并且使用了输入输出流 传递的参数还是用“name=XXX”这种硬编的字符串进行 ...
- Apache httpclient拦截器对请求进行签名
Apahce httpclient 提供HttpRequestInterceptor和HttpResponseInterceptor两种拦截器分别处理请求和响应数据,下面讲一下如何对http请求进行拦 ...
- Apache Tomcat如何高并发处理请求
介绍 作为常用的http协议服务器,tomcat应用非常广泛.tomcat也是遵循Servelt协议的,Servelt协议可以让服务器与真实服务逻辑代码进行解耦.各自只需要关注Servlet协议即可. ...
- 通过 Apache Commons HttpClient 发送 HTTPS 请求
1.通过 HTTPS 发送 POST 请求: 2.HTTPS 安全协议采用 TLSv1.2: 3. 使用代理(Proxy)进行 HTTPS 访问: 4.指定 Content-Type 为:applic ...
- 查看 Apache并发请求数及其TCP连接状态
查看 Apache并发请求数及其TCP连接状态 (2011-06-27 15:08:36) 服务器上的一些统计数据: 1)统计80端口连接数 netstat -nat|grep -i "80 ...
- HTTP POST请求的Apache Rewrite规则设置
最近自测后端模块时有个业务需求需要利用WebServer(我用的是Apache)将HTTP POST请求转发至后端C模块,后端处理后返回2进制加密数据.http post请求的url格式为: ...
- 查看 Apache并发请求数及其TCP连接状态【转】
查看 Apache并发请求数及其TCP连接状态 (2011-06-27 15:08:36) 服务器上的一些统计数据: 1)统计80端口连接数netstat -nat|grep -i "80& ...
- java模拟post请求发送json
java模拟post请求发送json,用两种方式实现,第一种是HttpURLConnection发送post请求,第二种是使用httpclient模拟post请求, 方法一: package main ...
随机推荐
- python 获取中文文件名的输出
#coding:utf8 if __name__ == '__main__': srcfile = u"D:/测试路径/测试文件.txt" f = open(srcfile.enc ...
- tiny4412-Uboot启动分析
一.从本质上将,引导转载程序至少应提供以下功能 (1)设置和初始化RAM (2)初始化一个串口 (3)检测机器类型(machine type) (4)设置内核标签列表(tag list) (5)调用内 ...
- js一种继承机制:用对象冒充继承构造函数的属性,用原型prototype继承对象的方法。
js一种继承机制:用对象冒充继承构造函数的属性,用原型prototype继承对象的方法. function ClassA(sColor) { this.color = sColor; } ClassA ...
- js 逻辑的短路运算
&& 与运算 同时为true,才为true: 表达式1为false,不用看表达式2: || 或运算 有一个为true,就为true: 表达式1为true,不用看表达式2: && ...
- webpack的3个路径配置项: assetsRoot、assetsSubDirectory、assetsPublicPath
在 vue-cli 构建模版的配置文件config.js中有assetsRoot,assetsSubDirectory和assetsPublicPath这三个路径配置项 assetsRoot:构建输出 ...
- FineUI导出Excel
1.[经验分享]导出Excel的乱码问题http://www.fineui.com/bbs/forum.php?mod=viewthread&tid=6326&highlight=Ex ...
- java 线程转换图
- 转 : jmeter分布式测试的坑
有关jmeter分布式测试的环境配置,大概就是那样,但是每次想要进行jmeter分布式测试的时候,总是会有各种奇怪的问题,下面整理了一些可能遇到的坑. 只要错误中出现:Error in rconfig ...
- 阿里巴巴Java开发手册-命名规约
1. [强制] 代码中的命名均不能以下划线或美元符号开始,也不能以下划线或美元符号结束.反例: _name / __name / $Object / name_ / name$ / Object$2. ...
- github 改位置
在设置里改位置后,先在本地库上右键"stop track this repo". 然后在在线库重新CLONE.