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 ...
随机推荐
- chrom 自带截屏用法
1 . F12调出控制台 2 . Ctrl+Shift+p 3 . 输入capture
- map和jsonObject 这2中数据结构之间转换
前台写json直接是:var array = [ ] ; 调用方法:array[index],若是对象,再[“key”] var obj = {''a'':123 , "b":&q ...
- leetcode:Reverse Integer【Python版】
1.在进入while之前,保证x是非负的: 2.符号还是专门用flag保存 =================== 3.另一思路:将integer转换成string,然后首位swap,直至中间: cl ...
- hangfire docker-compose 运行
hangfire 是一款基于.net 的任务调度系统 docker-compose 文件 version: '3' services: hangfire: image: direktchark/han ...
- 使用js 文件参数 以及IHttpModule实现服务验证asp.net 版的初步实现
接上面的文章,上面的主要是进行html 页面自己进行处理.但是对于进行asp.net 的开发者以及其他的就显的不太好了. 我的实现方式是使用IHttpModule 进行对于用户请求的带有参数的js文件 ...
- TweenMax.allTo
需要多个MC进行相同的缓动.比如下面这个游戏菜单.三个按钮的缓动是相同的,都缓动到同一个x坐标位置.然后同时有缓动出舞台. 如果有TweenLite实现的话,需要 if (is ...
- ZooKeeper 知识点
zookeeper 命令: 命令 说明 ./zkServer.sh start 启动ZooKeeper(终端执行) ./zkServer.sh stop 停止ZooKeeper(终端执行) ./zkC ...
- php 图片剪切
<?php /** * 图像裁剪 * @param $source_path 原图路径 * @param $target_width 需要裁剪的宽 * @param $target_height ...
- bzoj 4556 [Tjoi2016&Heoi2016]字符串——后缀数组+主席树
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=4556 本来只要查 ht[ ] 数组上的前驱和后继就行,但有长度的限制.可以二分答案解决!然后 ...
- Windows下 YOLOv3配置教程(YOLOv3项VS2013平台迁移的方法)
https://blog.csdn.net/maweifei/article/details/81150489