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 获取excel文件的所有sheet名字
当一个excel文件的sheet比较多时候, 这时候需要获取所有的sheet的名字. xl = pd.ExcelFile('foo.xls') xl.sheet_names # see all she ...
- 如何调优JVM
堆设置 -Xmx3550m:设置JVM最大堆内存 为3550M. -Xms3550m:设置JVM初始堆内存 为3550M.此值可以设置与-Xmx相同,以避免每次垃圾回收完成后JVM重新分配内存. -X ...
- loopback v4 特性
loopback 是一个api 服务框架,挺方便的,同时也已经演进了好几代了v4 有一些新功能的 支持 新特性 基于typescript/es2017 开发 openapi 驱动的rest api 开 ...
- 二叉搜索树的第k大的节点
题目 给定一颗二叉搜索树,请找出其中的第k大的结点. 思路 如果中序遍历一棵二叉搜索树,遍历序列的数值则是递增排序,因此只需中序遍历一个二叉搜索树即可. #include <iostream&g ...
- tomcat源码阅读之生命周期(LifeCycle)
一.事件机制流程: 1. 当外部事件源发生事件(比如点击了按钮,数据发生改变更新等)时,事件源将事件封装成事件对象Event: 2. 将事件对象交由对应的事件派发器Dispatcher ...
- FineUI Grid中WindowField根据列数据决定是否Enalble
前台页面Grid控件中设置OnPreRowDataBound属性,windowFile控件设置ID protected void Grid1_PreRowDataBound(object sender ...
- idea引入svn
刚想在idea看一个svn的项目代码,结果发现导入项目后,idea在右下角弹出了Event Log窗口,里面的红色小字 Can't use Subversion command line client ...
- WifiMonitor的事件发放
Wifi框架中WifiMonitor负责上报wpa_supplicant的消息给WifiStateMachine,WifiNative负责将WifiStateMachine的消息下发给wpa_supp ...
- ALGO-7_蓝桥杯_算法训练_逆序对
出处:http://blog.csdn.net/enjoying_science/article/details/44114035 (有难度,以后回来填坑) 阅读代码中: #include<st ...
- 【XMLHttpRequest】获取XMLHttpRequest
// 获取http请求 function getXMLHttpRequest() { req = false; //本地XMLHttpRequest对象 if (window.XMLHttpReque ...