java发送post 的json请求
package com.elink.estos.mq.mqmanager;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.URL;
import java.io.BufferedReader;
import java.io.InputStreamReader;
public class JavaPostJson2 {
final static String url = "http://localhost:1111/sendData";
final static String params = "{\"token\":\"12345\","
+ "\"appId\":\"20180628173051169c85d965d164ed9ab1281d22ff350ec19\"" + "\"appName\":\"test33\""
+ "\"classTopic\":\"112\"" + "\"eventTag\":\"22\"" + "}";
public static void main(String[] args) {
String res = post(url, params);
System.out.println(res);
}
/**
* 发送HttpPost请求
*
* @param strURL
* 服务地址
* @param params
* json字符串,例如: "{ \"id\":\"12345\" }" ;其中属性名必须带双引号<br/>
* @return 成功:返回json字符串<br/>
*/
public static String post(String strURL, String params) {
System.out.println(strURL);
System.out.println(params);
BufferedReader reader = null;
try {
URL url = new URL(strURL);// 创建连接
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setDoOutput(true);
connection.setDoInput(true);
connection.setUseCaches(false);
connection.setInstanceFollowRedirects(true);
connection.setRequestMethod("POST"); // 设置请求方式
// connection.setRequestProperty("Accept", "application/json"); // 设置接收数据的格式
connection.setRequestProperty("Content-Type", "application/json"); // 设置发送数据的格式
connection.connect();
//一定要用BufferedReader 来接收响应, 使用字节来接收响应的方法是接收不到内容的
OutputStreamWriter out = new OutputStreamWriter(connection.getOutputStream(), "UTF-8"); // utf-8编码
out.append(params);
out.flush();
out.close();
// 读取响应
reader = new BufferedReader(new InputStreamReader(connection.getInputStream(), "UTF-8"));
String line;
String res = "";
while ((line = reader.readLine()) != null) {
res += line;
}
reader.close();
//如果一定要使用如下方式接收响应数据, 则响应必须为: response.getWriter().print(StringUtils.join("{\"errCode\":\"1\",\"errMsg\":\"", message, "\"}")); 来返回
// int length = (int) connection.getContentLength();// 获取长度
// if (length != -1) {
// byte[] data = new byte[length];
// byte[] temp = new byte[512];
// int readLen = 0;
// int destPos = 0;
// while ((readLen = is.read(temp)) > 0) {
// System.arraycopy(temp, 0, data, destPos, readLen);
// destPos += readLen;
// }
// String result = new String(data, "UTF-8"); // utf-8编码
// System.out.println(result);
// return result;
// }
return res;
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return "error"; // 自定义错误信息
}
}
import java.io.IOException;import java.io.InputStream;import java.io.OutputStreamWriter;import java.net.HttpURLConnection;import java.net.URL;import java.io.BufferedReader;import java.io.InputStreamReader;
public class JavaPostJson2 {final static String url = "http://localhost:1111/sendData";// final static String params = "{\"token\":\"12345\","// + "\"appId\":\"20180628173051169c85d965d164ed9ab1281d22ff350ec19\"" // + "\"appName\":\"test33\""// + "\"classTopic\":\"112\"" // + "\"eventTag\":\"22\"" // + "\"msg\":{}"// + "}";final static String params = "{\"token\":\"12345\","+ "\"appId\":\"20180628173051169c85d965d164ed9ab1281d22ff350ec19\","+ "\"appName\":\"test33\","+ "\"classTopic\":\"112\","+ "\"eventTag\":\"22\""+ "}";
public static void main(String[] args) {
String res = post(url, params);System.out.println(res);
}
/** * 发送HttpPost请求 * * @param strURL * 服务地址 * @param params * json字符串,例如: "{ \"id\":\"12345\" }" ;其中属性名必须带双引号<br/> * @return 成功:返回json字符串<br/> */public static String post(String strURL, String params) {System.out.println(strURL);System.out.println(params);BufferedReader reader = null;try {URL url = new URL(strURL);// 创建连接HttpURLConnection connection = (HttpURLConnection) url.openConnection();connection.setDoOutput(true);connection.setDoInput(true);connection.setUseCaches(false);connection.setInstanceFollowRedirects(true);connection.setRequestMethod("POST"); // 设置请求方式// connection.setRequestProperty("Accept", "application/json"); // 设置接收数据的格式connection.setRequestProperty("Content-Type", "application/json"); // 设置发送数据的格式connection.connect();OutputStreamWriter out = new OutputStreamWriter(connection.getOutputStream(), "UTF-8"); // utf-8编码out.append(params);out.flush();out.close();// 读取响应reader = new BufferedReader(new InputStreamReader(connection.getInputStream(), "UTF-8"));String line;String res = "";while ((line = reader.readLine()) != null) {res += line;}reader.close();
return res;} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}return "error"; // 自定义错误信息}
}
java发送post 的json请求的更多相关文章
- JAVA发送http get/post请求,调用http接口、方法
import java.io.BufferedReader; import java.io.IOException;import java.io.InputStream; import java.io ...
- Java发送http get/post请求,调用接口/方法
由于项目中要用,所以找了一些资料,整理下来. GitHub地址: https://github.com/iamyong 转自:http://blog.csdn.net/capmiachael/a ...
- JAVA发送http GET/POST请求的两种方式+JAVA http 请求手动配置代理
java发送http get请求,有两种方式. 第一种用URLConnection: public static String get(String url) throws IOException { ...
- Java 发送Get和Post请求
package com.htpt.superviseServices.dm.util; import java.io.BufferedReader; import java.io.IOExceptio ...
- Java 发送http GET/POST请求
最近项目里面需要用到Java发送http请求,由于发送https请求有点复杂,暂时不考虑 HttpURLConnection HttpURLConnection是一种多用途.轻量极的HTTP客户端,使 ...
- Java发送get及post请求工具方法
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import ...
- [java,2018-01-16] HttpClient发送、接收 json 请求
最近需要用到许多在后台发送http请求的功能,可能需要发送json和xml类型的数据. 就抽取出来写了一个帮助类: 首先判断发送的数据类型是json还是xml: import org.dom4j.Do ...
- java发送GET和post请求
package com.baqingshe.bjs.util; import java.io.BufferedReader; import java.io.IOException; import ja ...
- 【工具】java发送GET、POST请求
前项目使用这种HTTP的方式进行数据交互,目前已更换数据交互方式,但是作为接口提供调用来说还是比较简洁高效的: 总体流程就是: 1.发送HTTP请求 2.获取返回的JSON对象 3.JSON转换 pa ...
随机推荐
- HttpModule和HttpHandler -- 系列文章
ASP.NET 生命周期 在ASP.Net2.0中使用UrlRewritingNet实现链接重写 IHttpModule实现URL重写 使用IHttpHandler防盗链 HttpModule,Htt ...
- Python 函数 -slice()
功能: slice() 函数实现切片对象,主要用在切片操作函数里的参数传递.返回一个切片对象. 语法: class slice(stop) class slice(start, stop[, step ...
- bzoj 1085 [SCOI2005]骑士精神——IDA*
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=1085 迭代加深搜索. 估价函数是为了预计步数来剪枝,所以要优于实际步数. 没错,不是为了确定 ...
- Google Cloud VM上在线扩硬盘
Google Cloud VM是可以在线扩展Disk的大小的. 一.创建VM和磁盘 比如我有一台VM,附加了一块Disk,大小是120GB.如下图: 在VM中进行格式化: mkfs.ext4 -F / ...
- 1050 String Subtraction
题意:给出两个字符串s1和s2,在s1中删去s2中含有的字符. 思路:注意,因为读入的字符串可能有空格,因此用C++的getline(cin,str).PAT系统迁移之后C语言中的gets()函数被禁 ...
- thinkphp实现多个子查询语句
sql语句博大精深 理解好sql语句,就能用好thinkphp等框架中的数据库操作 原sql SELECT a.*,b.* from (SELECT a.id as opener_id,a.name, ...
- Centos7 第三方仓库 yum 方式安装 PHP7.2
1.卸载原先安装的PHP yum remove php rpm -qa|grep php #列出所有的php相关的rpm包 rpm -e xxx #xxx指的是上一个命令列出的rpm包的包名,复制即可 ...
- AUC ROC PR曲线
ROC曲线: 横轴:假阳性率 代表将负例错分为正例的概率 纵轴:真阳性率 代表能将正例分对的概率 AUC是ROC曲线下面区域得面积. 与召回率对比: AUC意义: 任取一对(正.负)样本,把正样本预测 ...
- leetcode897
这道题用C++来写,在本地执行正常,但是使用OJ判断输出结果是空,暂时不清楚原因.代码如下: class Solution { public: vector<int> V; //中序遍历 ...
- 有关DNS
单位的域控制器和下面客户端的连接一直有问题,时常会找不到服务器连接: 错误基本显示为:没有可以登录的服务器 无法连接seed.com域 后来发现,是DNS解析的问题,单位使用了外部解析地址202.96 ...