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 ...
随机推荐
- dbt 的知识文档管理
dbt 支持docs的管理,可以方便进行分享,以及大家的可视化工作 有一篇文档讲的特别好分析了知识共享,知识管理的重要性(dbt 对应公司的ceo) https://blog.fishtownanal ...
- zz 牛人啊
http://www.newsmth.net/nForum/#!article/CouponsLife/184517019:57:33cutepig 2015/6/9 19:57:33 http:// ...
- Oracle ASM 详解
ASM:Automatic Storage Management, 是Oracle 主推的一种面向Oracle的存储解决方案, ASM 和 RDBMS 非常相似,ASM 也是由实例和文件组成, 也可以 ...
- hybrid app、react-native 区别
hybrid app.react-native 区别: 项目 hybrid app react-native 组件 用HTML.CSS.JavaScript实现页面的制作,然后运行在Webview上( ...
- python3 获取int最大值
python2 中获取int最大值 import sys i = sys.maxint print i 但是在python3中,报错: AttributeError: module 'sys' has ...
- php生成迷宫和迷宫寻址算法实例
较之前的终于有所改善.生成迷宫的算法和寻址算法其实是一样.只是一个用了遍历一个用了递归.参考了网上的Mike Gold的算法. <?php //zairwolf z@cot8.com heade ...
- html 子元素和父元素都监听了 click 事件,点击子元素时为何先触发的是父元素的 click 事件?
先上一段代码,点击子元素时先触发的是父元素的 click 事件 <html> <head> <script type="text/javascript" ...
- sql having 函数 按匿名字段作为条件进行查询
今天写sql 遇到一个问题 SELECT a.*, count(b.id) AS nums FROM a LEFT JOIN b ON a.id=b.a_id WHERE nums>1 这时候会 ...
- 2013-7-27 802.1X学习
最近搭了企业级加密的server 2003服务器,教程完全google,无任何自主创新.折腾了一周,总算搞定了,同时也验证了server 2003下的TLS和PEAP0加密算法是正常的. 至于搭建se ...
- C++进阶--const变量
//############################################################# // const // - 编译时的限制:一个对象不能被修改 // in ...