Java 用HTTP的方式发送JSON报文请求
前言:
项目调用第三方接口时,通常是用socket或者http的通讯方式发送请求:http 为短连接,客户端发送请求都需要服务器端回送响应,请求结束后,主动释放链接。Socket为长连接:通常情况下Socket 连接就是 TCP 连接,因此 Socket 连接一旦建立,通讯双方开始互发数据内容,直到双方断开连接。下面介绍HTTP的方式发送和接收JSON报文。
需求:
用HTTP的方式,向URL为127.0.0.1:8888地址发送json报文,返回的结果也是json报文。
主要代码如下:
String resp= null;
JSONObject obj = new JSONObject();
obj.put("name", "张三");
obj.put("age", "");
String query = obj.toString();
log.info("发送到URL的报文为:");
log.info(query);
try {
URL url = new URL("http://127.0.0.1:8888"); //url地址 HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setDoInput(true);
connection.setDoOutput(true);
connection.setRequestMethod("POST");
connection.setUseCaches(false);
connection.setInstanceFollowRedirects(true);
connection.setRequestProperty("Content-Type","application/json");
connection.connect(); try (OutputStream os = connection.getOutputStream()) {
os.write(query.getBytes("UTF-8"));
} try (BufferedReader reader = new BufferedReader(
new InputStreamReader(connection.getInputStream()))) {
String lines;
StringBuffer sbf = new StringBuffer();
while ((lines = reader.readLine()) != null) {
lines = new String(lines.getBytes(), "utf-8");
sbf.append(lines);
}
log.info("返回来的报文:"+sbf.toString());
resp = sbf.toString(); }
connection.disconnect(); } catch (Exception e) {
e.printStackTrace();
}finally{
JSONObject json = (JSONObject)JSON.parse(resp);
}
网上还有一种拼json发送报文的方式,也把代码分享出来:
String resp = null;
String name = request.getParameter("userName");
String age = request.getParameter("userAge");
String query = "{\"name\":\""+name+"\",\"age\":\""+age+"\"}"; try {
URL url = new URL("http://10.10.10.110:8888"); //url地址 HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setDoInput(true);
connection.setDoOutput(true);
connection.setRequestMethod("POST");
connection.setUseCaches(false);
connection.setInstanceFollowRedirects(true);
connection.setRequestProperty("Content-Type","application/json");
connection.connect(); try (OutputStream os = connection.getOutputStream()) {
os.write(query.getBytes("UTF-8"));
} try (BufferedReader reader = new BufferedReader(
new InputStreamReader(connection.getInputStream()))) {
String lines;
StringBuffer sbf = new StringBuffer();
while ((lines = reader.readLine()) != null) {
lines = new String(lines.getBytes(), "utf-8");
sbf.append(lines);
}
log.info("返回来的报文:"+sbf.toString());
resp = sbf.toString(); }
connection.disconnect(); } catch (Exception e) {
e.printStackTrace();
}finally{
JSONObject json = (JSONObject)JSON.parse(resp);
}
两种方式其实都是一样的。
Java 用HTTP的方式发送JSON报文请求的更多相关文章
- java 模拟http请求,通过流(stream)的方式,发送json数据和文件
发送端: /** * 以流的方式 * 发送文件和json对象 * * @return */ public static String doPostFileStreamAndJsonObj(String ...
- java使用POST发送soap报文请求webservice返回500错误解析
本文使用JAX-WS2.2编译webservice,并使用HttpUrlConnection的POST方式对wsdl发送soap报文进行请求返回数据, 对错误Server returned HTTP ...
- volley用法之 以post方式发送 json 参数
需求是这样 我们需要发送一个post请求向服务器要参数.要求是发送的post参数也要是json格式. 简单一点的是这样的: 如果要发送的是这样简单的json格式,我们可以简单的使用map来实现: Re ...
- ABAP接口之Http发送json报文
abap 调用http 发送 json 测试函数 SE11创建结构:zsmlscpnotice SE37创建函数:zqb_test_http_fuc1 FUNCTION zqb_test_http_f ...
- HttPclient 以post方式发送json
使用HttpClient 以POST的形式发送json字符串 步骤: 1.url .parameters 2.创建httpClient对象 3.创建HttpPost对象 4.为post对象设置参数 5 ...
- 【PostMan】1、Postman 发送json格式请求
Postman 是一个用来测试Web API的Chrome 外挂软件,可由google store 免费取得并安装于Chrome里,对于有在开发Web API的开发者相当有用,省掉不少写测试页面呼叫的 ...
- HttpClient通过Post方式发送Json数据
服务器用的是Springmvc,接口内容: @ResponseBody @RequestMapping(value="/order",method=RequestMethod.PO ...
- Java 通过HttpClient Post方式提交json请求
package com.sinosoft.ap.harmfullibrary.util; /** * 发送post请求 */import net.sf.json.JSONObject; import ...
- springboot使用RestTemplate以post方式发送json字符串参数(以向钉钉机器人发送消息为例)
使用springboot之前,我们发送http消息是这么实现的 我们用了一个过时的类,虽然感觉有些不爽,但是出于一些原因,一直也没有做处理,最近公司项目框架改为了springboot,springbo ...
随机推荐
- Java知多少(27)继承的概念与实现
继承是类与类之间的关系,是一个很简单很直观的概念,与现实世界中的继承(例如儿子继承父亲财产)类似. 继承可以理解为一个类从另一个类获取方法和属性的过程.如果类B继承于类A,那么B就拥有A的方法和属性. ...
- excel 2007 无法输入中文
解决方法: 1.32位系统:找到C:\Program Files\Common Files\Microsoft Shared\OFFICE12\Office Setup Controller,将这个文 ...
- Ubuntu下在Apache中运行Keystone
最近一次从Github上更新Keystone的代码后,发现原来bin/keystone-all和bin/keystone-manage都不见了,取而代之的是keystone/cmd/目录下的all.p ...
- HRMS文件解析2
returntablefieldColorFilterGray()函数在/lib/select_menu.php文件中,函数如下: function returntablefieldColorFilt ...
- 微信小游戏的本地缓存和清除的使用 (text.js image.js file-util.js)
参考: 微信小游戏,文件系统 UpdateManager-小游戏 一.Egret提供的本地缓存工具类( 备注:新版本进行了修改,并增加了sound.js等) 在微信小游戏项目中,Egret提供了fil ...
- jQuery跨域调用Web API
我曾经发表了一篇关于如何开发Web API的博客,链接地址:http://www.cnblogs.com/guwei4037/p/3603818.html.有朋友说开发是会开发了,但不知道怎么调用啊? ...
- SVM 核方法
在 SVM 中引入核方法便可使得 SVM 变为非线性分类器,给定非线性可分数据集 $\left \{ (x_i,y_i)\right\}_{i=1}^N$,如下图所示,此时找不到一个分类平面来将数据分 ...
- day_5.10py 爬妹子图片 mm131
#目前学的爬虫还有潭州教育的直播课,都是千篇一律的requests urllib 下面这个也是,还没有我后面的下载网易云歌单爽来都用到多线程了不过可以用协程,完全异步 1 #!/usr/bin/env ...
- C - Building Fence
Long long ago, there is a famous farmer named John. He owns a big farm and many cows. There are two ...
- centos下nginx的启动
今天下载了个nginx,编译安装之后,无法启动,以前的启动方法无法实现(services nginx start), 在网上找了半天,才找到一个方法,与大家分享: 命令如下: 启动:/usr/loca ...