post json数据到springmvc中并获取http请求的内容
来源:互联网 发布:淘客采集上传淘宝店铺 编辑:程序博客网 时间:2024/06/01 09:21
需求:不使用浏览器进行访问,而是希望使用GroboUtils-5进行多线程测试http post json数据的测试(使用GroboUtils-5进行多线程测试参考:使用GroboUtils多线程并发请求测试springmvc controller)
这个需求带来两个问题:1是如何使用java.net下的API实现向springmvc postjson数据;2是在访问springmvc如何显示这些数据,以方便我们看到post的数据是否正确。
解决方案:
直接上代码
对于问题1:
public class BellyJsonPoster { private URL url; public BellyJsonPoster(URL url) { if (!url.getProtocol().toLowerCase().startsWith("http")) { throw new IllegalArgumentException( "Posting only works for http URLs"); } this.url = url; } public InputStream post() throws IOException { // open the connection and prepare it to POST HttpURLConnection connection = (HttpURLConnection) url.openConnection(); //设置http连接属性 connection.setDoOutput(true); connection.setDoInput(true); connection.setRequestMethod("POST"); // 可以根据需要 提交 GET、POST、DELETE、INPUT等http提供的功能 connection.setUseCaches(false); connection.setInstanceFollowRedirects(true); //设置http头 消息//设定 请求格式 json,也可以设定xml格式的 connection.setRequestProperty("Content-Type", "application/json"); //设定 请求格式 xml, //connection.setRequestProperty("Content-Type", "text/xml");//设定响应的信息的格式为 json,也可以设定xml格式的 connection.setRequestProperty("Accept", "application/json");//特定http服务器需要的信息,根据服务器所需要求添加//connection.setRequestProperty("X-Auth-Token","xx"); //添加 请求内容 JSONObject user = new JSONObject(); user.put("id", "1"); user.put("name", "bellychang"); user.put("pwd", "123"); try (OutputStreamWriter out = new OutputStreamWriter(connection.getOutputStream(), "UTF-8")) { // The POST line, the Content-type header, // and the Content-length headers are sent by the URLConnection. // We just need to send the data out.write(user.toString()); out.flush(); } // Return the response return connection.getInputStream(); }}
对于问题2:写了一个工具类来实现,这样在Contorller中调用这个方法就可以显示Header和body的内容
public class BellyWebUtil { public static void readHttpHeaders(HttpServletRequest request) { Enumeration headerNames = request.getHeaderNames(); if (headerNames != null) { while (headerNames.hasMoreElements()) { System.out.println(headerNames.nextElement().toString() + ":" + request.getHeader(headerNames .nextElement().toString())); } } } public static void readHttpBody(HttpServletRequest request) throws IOException { //读取input流 BufferedReader br = new BufferedReader(new InputStreamReader((ServletInputStream) request.getInputStream(), "utf-8")); StringBuffer sb = new StringBuffer(""); String temp; while ((temp = br.readLine()) != null) { sb.append(temp); } br.close(); System.out.println(sb.toString()); } public static void readHttpRequest(HttpServletRequest request) throws IOException { readHttpHeaders(request); readHttpBody(request); }}参考:
java 发送 json、xml格式的 http请求,并读取响应response内容实例
java接收http请求body中的json数据
阅读全文
0 0
- post json数据到springmvc中并获取http请求的内容
- 解析HTTP POST方式请求的xml数据、本地XML文件数据, 并返回JSON字符串
- 发送http post请求json数据到后台
- post请求获取json数据
- http的post方式连接服务器,发送数据到服务端,并获取服务端的数据
- HTTP POST请求并将请求参数设置成请求体添加到请求中
- httpclient的post请求并处理json格式响应数据
- java http 请求之post json数据
- java http 请求之post json数据
- HTTP协议post请求返回json数据
- php 发起 post http请求 ,并传body内容 到另一个php页面
- java 发送http协议的POST请求获取数据
- Java获取post请求json数据
- jmeter添加http请求、java请求,将返回的json数据提取出来并将结果追加到一个文件
- 处理HTTP请求并返回JSON数据的类
- 获取post请求的数据
- http请求返回并解析json数据
- http请求返回并解析json数据
- Raft一致性算法
- MFC 在某些特定条件下改变控件内文本的颜色的方法
- spring 相关注解
- codeforces813C The Tag Game
- Kotlin学习笔记(二)---从零学习Kotlin
- post json数据到springmvc中并获取http请求的内容
- NET中WebBrowser控件内部页面的JS代码与外部C#代码的相互调用
- 设计模式 适配器模式 以手机充电器为例
- 硬币排成线
- 微信小程序如何推广
- java+JavaScript 实现 非对称加密 rsa
- Android研究院之应用开发线程池的经典使用
- TCP--server
- PHP Mysqli的query返回值探讨