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 ...
随机推荐
- (转)Linux 定时关机、休眠命令
立刻关机:sudo haltsudo init 0 sudo shutdown -h nowsudo shutdown -h 0....定时/延时关机:sudo shutdown -h 19:3019 ...
- WPF控件NumericUpDown (转)
WPF控件NumericUpDown示例 (转载请注明出处) 工具:Expression Blend 2 + Visual Studio 2008 语言:C# 框架:.Net Framework 3. ...
- MySQL Disk--SSD 特性
======================================================================= SSD 特性 .随机读能力非常好,连续读性能一般,但比普 ...
- error MSB3073: 命令“regsvr32 /s /c:VCEnd”已退出,代码为 3
版权声明:博客地址:blog.csdn.net/x356982611,未经同意不得转载,不得转载,不得转载 https://blog.csdn.net/x356982611/article/detai ...
- Oracle数据泵的使用
几乎所有DBA都熟悉oracle的导出和导入实用程序,它们将数据装载进或卸载出数据库,在oracle database 10g和11g中,你必须使用更通用更强大的数据泵导出和导入(Data Pump ...
- sklearn, Numpy以及Pandas
pandas里面的对于数据操作比如where,drop以及dropna等都会有一个属性:inplace,这个单词意思是原地,如果inplace=true代表数据本身要执行该操作:如果inplace=f ...
- POJ3254Corn Fields——状态压缩dp
题目:http://poj.org/problem?id=3254 1.枚举行: 2.把有影响的“放不放牛”加入参数中,用二进制数表示该位置放不放牛,再用十进制数表示二进制数: 3.优美的预处理lis ...
- mySQL 教程 第8章 视图
创建视图的目的 简单 隐藏数据复杂性 安全 可以对视图授权 数据独立 可以屏蔽表结构变化对用户的影响,比如增加列,更改列名 创建视图 1. 创建单表视图 以下视图显示JAVA班的学生姓名.身份证号和班 ...
- 打开Visual Studio 2012的解决方案 连接 Dynamics CRM 2011 的Connect to Dynamics CRM Server 在其工具下没有显示
一.使用TFS 代码管理,发现Visual Studio 2012 菜单栏 工具下的Connect to Dynamics CRM Server 没有显示. 平常打开VS下的工具都会出现Connect ...
- C#利用zxing.net操作二维码和条形码
下载地址:http://zxingnet.codeplex.com/ zxing.net是.net平台下编解条形码和二维码的工具,使用非常方便. 首先下载二进制dll文件,引入工程: 代码: usin ...