rest接口webservice接口利用http请求方式发送数据
所需依赖
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.3</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpcore</artifactId>
<version>4.4.10</version>
</dependency>
package com.sinoservices.bms.bbl.common.util; import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.nio.charset.Charset; import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature; /**
*
* @Description http调用工具类
* @author Lynch.Feng
* @Date 2018年10月26日 下午3:53:03
* @version 1.0.0
*/
public class HttpUtil {
private static final Logger logger = LoggerFactory.getLogger(HttpUtil.class); private HttpUtil() { } private static CloseableHttpClient httpClient; static {
PoolingHttpClientConnectionManager cm = new PoolingHttpClientConnectionManager();
cm.setMaxTotal(100);
cm.setDefaultMaxPerRoute(20);
cm.setDefaultMaxPerRoute(50);
httpClient = HttpClients.custom().setConnectionManager(cm).build();
} public static String get(String url) {
CloseableHttpResponse response = null;
BufferedReader in = null;
String result = "";
try {
HttpGet httpGet = new HttpGet(url);
RequestConfig requestConfig =
RequestConfig.custom().setConnectTimeout(30000).setConnectionRequestTimeout(30000).setSocketTimeout(30000).build();
httpGet.setConfig(requestConfig);
httpGet.setConfig(requestConfig);
httpGet.addHeader("Content-type", "application/json; charset=utf-8");
httpGet.setHeader("Accept", "application/json");
response = httpClient.execute(httpGet);
in = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
StringBuilder sb = new StringBuilder();
String line = "";
while ((line = in.readLine()) != null) {
sb.append(line);
}
in.close();
result = sb.toString();
} catch (IOException e) {
logger.error("http request error", e);
} finally {
try {
if (null != response) {
response.close();
}
} catch (IOException e) {
logger.error("close response error", e);
}
}
return result;
} public static String postJson(String url, String jsonString) {
CloseableHttpResponse response = null;
BufferedReader in = null;
String result = "";
try {
HttpPost httpPost = new HttpPost(url);
RequestConfig requestConfig =
RequestConfig.custom().setConnectTimeout(30000).setConnectionRequestTimeout(30000).setSocketTimeout(30000).build();
httpPost.setConfig(requestConfig);
httpPost.setConfig(requestConfig);
httpPost.addHeader("Content-type", "application/json; charset=utf-8");
httpPost.setHeader("Accept", "application/json");
httpPost.setEntity(new StringEntity(jsonString, Charset.forName("UTF-8")));
response = httpClient.execute(httpPost);
in = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
StringBuilder sb = new StringBuilder();
String line = "";
while ((line = in.readLine()) != null) {
sb.append(line);
}
in.close();
result = sb.toString();
} catch (IOException e) {
logger.error("http request error", e);
} finally {
try {
if (null != response) {
response.close();
}
} catch (IOException e) {
logger.error("close response error", e);
}
}
return result;
}
}
rest接口webservice接口利用http请求方式发送数据的更多相关文章
- C#带cookie Post和Get方式发送数据,保持cookie
在实际编程中,可能需要读取特定网页的信息,但很多网站需要用户登录后,才能够获取相关的页面内容,这就需要编程者先临时存储当前的cookie,在C#中可以使用CookieContainer 对象来保存登录 ...
- Android使用HttpClient以Post、Get请求服务器发送数据的方式(普通和json)
讲这个之前,我们先来说说get和post两种请求的区别吧!!! 1. GET提交的数据会放在URL之后,以?分割URL和传输数据,参数之间以&相连,如EditPosts.jsp?name=te ...
- Iris请求方式和数据返回类型
1. Iris起服务 package main import "github.com/kataras/iris" func main() { //1.创建app结构体对象 app ...
- Android使用HttpUrlConnection请求服务器发送数据详解
HttpUrlConnection是java内置的api,在java.net包下,那么,它请求网络同样也有get请求和post请求两种方式.最常用的Http请求无非是get和post,get请求可以获 ...
- Ajax使用post方式发送数据注意事项
Ajax使用post方式给服务器传递数据时,需要将传递的字符串转化为模拟from表单发送数据的XML格式 在open之后奢姿头协议信息,模拟from表单传递数据 xhr.setRequestHeade ...
- get、post请求方式在postman中使用步骤
1.get请求方式:不需要借助任何工具,在浏览器里面就可以发送请求,直接在浏览器里面输入访问 url?参数名=参数值 url?parma=abc&name=abcd 2.post请 ...
- TR-069_Amendment-4:附录G.穿越NAT网关的连接请求方式
注意:这种机制只适用于RFC 3489[21]中定义的经典STUN,RFC 5389引入后,这个机制已经过时.这个机制不是设计用于RFC 5389中定义的STUN.IPv6部署要么不使用NAT,要么以 ...
- 使用loadrunner编写webservice接口请求
1.使用工具: loadrunner12,本实例截图中都是loadrunner12工具 2.操作步骤: 1).新建脚本,选择Web Services协议: 2).选择工具栏: 3).点击Import, ...
- 利用Fiddler拦截接口请求并篡改数据
近期在测试一个下单的项目,出于安全角度考虑,测试了一个场景,那就是利用工具对接口进行拦截并篡改数据.将接口一拦截并篡改数据后,发现收货满满.开发默默接受了我的建议,并对代码进行了修改. 对于fiddl ...
随机推荐
- HTML5 full-screen全屏API
这篇文章纯属记录,非常感谢张鑫旭大神的demo 原文地址: http://www.zhangxinxu.com/study/201210/html5-full-screen-api.html 代码 C ...
- rownum用法
对于rownum来说它是oracle系统顺序分配为从查询返回的行的编号,返回的第一行分配的是1,第二行是2,依此类推,这个伪字段可以用于限制查询返回的总行数,且rownum不能以任何表的名称作为前缀. ...
- Lightgbm 随笔
lightGBM LightGBM 是一个梯度 boosting 框架,使用基于学习算法的决策树.它可以说是分布式的,高效的,有以下优势: 更快的训练效率 低内存使用 更高的准确率 支持并行化学习 可 ...
- 开源微信管家平台——JeeWx 捷微4.0 微服务版本发布,全新架构,全新UI,提供强大的图文编辑器
JeeWx捷微4.0 微服务版本发布^_^ 换代产品(全新架构,全新UI,提供强大的图文编辑器) JEEWX 从4.0版本开始,技术架构全新换代,采用微服务架构,插件式开发,每个业务模块都是独立的 ...
- Golang源码探索(二) 协程的实现原理(转)
Golang最大的特色可以说是协程(goroutine)了, 协程让本来很复杂的异步编程变得简单, 让程序员不再需要面对回调地狱,虽然现在引入了协程的语言越来越多, 但go中的协程仍然是实现的是最彻底 ...
- 关于Qrc文件的用法
在python文件xxx.py中调用资源文件,一般来说,需要将资源放在xxx.py的相同目录下:然而,当在xxx.py下建立一个统一目录/rec则需要建立xxx.qrc文件才能让xxx.py调用,调用 ...
- 安装SQL server 2008 R2和QL server 2008,与SQL server 2008升级SQL server 2008 R2
安装SQL server 2008 R2和由SQL server 2008升级SQL server 2008 R2 前提条件: 由SQL server2008 升级SQL server2008 R2 ...
- 20175213《Java程序设计》实验二Java面向对象程序设计实验报告
实验内容 1.初步掌握单元测试和TDD 2.理解并掌握面向对象三要素:封装,继承,多态 3.初步掌握UML建模 4.熟悉S.O.L.I.D原则 5.了解设计模式 知识总结 1.伪代码 2.产品代码: ...
- 深入理解C++11【4】
[深入理解C++11[4]] 1.基于范围的 for 循环 C++98 中需要告诉编译器循环体界面范围.如for,或stl 中的for_each: int main() { ] = { , , , , ...
- python之字符串拼接:%和format
使用百分号拼接字符串: 例如: msg='i am %s my hobby is...' %'abc' print(msg) 如果需要用2个%s呢?就使用括号例如: msg='I am %s my h ...