SpringMVC获取HttpClient 请求的数据
package com.nnk.upstream.controller;
import org.springframework.util.StreamUtils;
import javax.servlet.ReadListener;
import javax.servlet.ServletInputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper;
import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
/**
* Created with IntelliJ IDEA.
* User: y
* Date: 2016/6/12
* Time: 9:30
* email: xxydliuy@163.com
* To change this template use File | Settings | File Templates.
*/
public class MyHttpServletRequestWrapper extends HttpServletRequestWrapper {
private byte[] body;
public MyHttpServletRequestWrapper(HttpServletRequest request) throws IOException {
super(request);
body = StreamUtils.copyToByteArray(request.getInputStream());
String contentType = request.getContentType();
System.out.println("contentType:" + contentType);
System.out.println("body length:" + body.length);
}
@Override
public ServletInputStream getInputStream() throws IOException {
final ByteArrayInputStream bais = new ByteArrayInputStream(body);
return new ServletInputStream() {
@Override
public int read() throws IOException {
return bais.read();
}
@Override
public boolean isFinished() {
return false;
}
@Override
public boolean isReady() {
return false;
}
@Override
public void setReadListener(ReadListener readListener) {
}
};
}
@Override
public BufferedReader getReader() throws IOException {
return new BufferedReader(new InputStreamReader(getInputStream()));
}
}
[quote]过滤器配置[/quote]
package com.nnk.upstream.controller;
import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
/**
* Created with IntelliJ IDEA.
* User: y
* Date: 2016/6/12
* Time: 9:35
* email: xxydliuy@163.com
* To change this template use File | Settings | File Templates.
*/
@WebFilter(filterName = "HttpReplaceFilter",urlPatterns="/*")
public class HttpReplaceFilter implements Filter {
@Override
public void init(FilterConfig filterConfig) throws ServletException {
}
@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
ServletRequest requestWrapper = null;
if(request instanceof HttpServletRequest) {
if(((HttpServletRequest) request).getMethod().equalsIgnoreCase("post")) {
requestWrapper = new MyHttpServletRequestWrapper((HttpServletRequest) request);
}
}
if(null == requestWrapper) {
chain.doFilter(request, response);
} else {
chain.doFilter(requestWrapper, response);
}
}
@Override
public void destroy() {
}
}
[quote]springboot 对过滤器的支持需要使用@ServletComponentScan相关注解[/quote]
@Configuration
@ComponentScan("com.nnk.upstream")
@EnableAutoConfiguration
@ImportResource("classpath:/context/bean.xml")
@ServletComponentScan
public class SampleIntegrationApplication extends SpringBootServletInitializer {
public static final Logger LOGGER = Logger.getLogger(SampleIntegrationApplication.class);
public static String IntanceKey;
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(SampleIntegrationApplication.class);
}
public static void main(String[] args) throws Exception {
SpringApplication.run(SampleIntegrationApplication.class, args);
}
}
[quote]使用httpClient请求Url会发现无法获取到流数据,如果是contentType:application/x-www-form-urlencoded; charset=UTF-8但是如果是contentType:text/plain; charset=GBK则可以获取流数据。流数据只能被读取一次,而且跟请求类型有关系[/quote]
package com.nnk.redis;
import com.nnk.upstream.core.ConfigContextManager;
import com.nnk.upstream.entity.parterner.CallbackRequest;
import com.nnk.upstream.util.ReflectUtils;
import com.nnk.upstream.util.SignSecurityUtlis;
import com.nnk.upstream.vo.InterfaceConfig;
import com.nnk.utils.http.exception.NetWorkException;
import com.nnk.utils.http.interfaces.HttpData;
import com.nnk.utils.http.utils.HttpClientUtils;
import org.apache.http.HttpEntity;
import org.apache.http.entity.StringEntity;
import org.apache.log4j.Logger;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.web.WebAppConfiguration;
import java.nio.charset.Charset;
/**
* Created with IntelliJ IDEA.
* User: y
* Date: 2016/4/25
* Time: 9:49
* email: xxydliuy@163.com
* To change this template use File | Settings | File Templates.
*/
@RunWith(SpringJUnit4ClassRunner.class)
@WebAppConfiguration
@ContextConfiguration(locations = {"classpath:context/bean.xml" })
public class CallbackTest {
private Logger logger = Logger.getLogger(CallbackTest.class);
@Autowired
private HttpClientUtils httpClientUtils;
@Autowired
private ConfigContextManager configContextManager;
@Test
public void testCallbackSuccess() throws NetWorkException {
CallbackRequest request = new CallbackRequest();
request.setOrderStatus("SUCCESS");
request.setRespCode("0000");
request.setRealValue("100");
request.setOemorderId("oemH89898709909");
request.setSendorderId("29038297439284320");
request.setMerchantNo("2000000011");
InterfaceConfig config = configContextManager.searchConfigContext("20000009");
request.setRespMsg("成功");
String signStr = ReflectUtils.getkeyValueString(request,"=","&","sign");
String sign = SignSecurityUtlis.sign(signStr,"123456");
request.setSign(sign);
String Resp = httpClientUtils.doPost("http://localhost:8099/Upstream/coustomizeCallback?merid=200000009",request);
}
@Test
public void testCallbackSuccess1() throws NetWorkException {
String Resp = httpClientUtils.doPost("http://localhost:8099/Upstream/coustomizeCallback?merid=200000009",new HttpData() {
@Override
public HttpEntity getPostdata() {
return new StringEntity("a=a&b=b", Charset.defaultCharset());
}
@Override
public String getGetdata() {
return null;
}
});
}
}
[quote]关于SpringMVc获取参数:请参看:http://json20080301.iteye.com/blog/1874074?utm_source=tuicool;
http://hw1287789687.iteye.com/blog/2199295[/quote]
SpringMVC获取HttpClient 请求的数据的更多相关文章
- php获取 POST请求的数据
普通键值对的数据: $_POST[‘username’]; // 获取 username的信息: $_REQUEST; //则会获取 整个请求中的键值对,返回结果为数组: 如果是,流数据,则需要使用: ...
- jquery中获取ajax请求返回数据的方法
function getPageTotalAndDataTotal(page) { //设置一个变量用于接收ajax返回的值 var pageTotal = 0; // 获取页数与数据总数 $.aja ...
- httpclient 请求 json 数据
基于\httpcomponents-client-4.5.5需要引入相关jar包如下: 必须导入commons-logging-1.2.jar,否则会提示 json api接口地址: https:// ...
- springMVC中接收请求参数&&数据转发
### 1. 接收请求参数 #### 1.1. [不推荐] 通过HttpServletRequest获取请求参数 假设存在: <form action="handle_login.do ...
- android 获取http请求json数据
package com.my.gethttpjsondata; import java.io.BufferedReader;import java.io.ByteArrayOutputStream;i ...
- Servlet Struts2 SpringMVC 获取参数与导出数据 方法比较
servlet中通过request.getParameter()从网页获取参数 通过request session servletContext几个域对象的setAttribute(String ,O ...
- 在SpringMVC获取客户端传递的数据的方式
在处理请求的方法中,加入相对应的形参,保证形参参数名和传递的数据的参数名保持一致,就能够自动赋值 value:当不满足赋值条件时,可以使用value属性,指定映射关系 required:设置形参是否必 ...
- HttpClient请求网络数据的Post请求
new Thread(){ public void run() { try { //获得输入框内容 ...
- SpringMVC跨重定向请求传递数据
(1)使用URL模板以路径变量和查询参数的形式传递数据(一些简单的数据) @GetMapping("/home/index") public String index(Model ...
随机推荐
- Linux crontab 每5秒钟执行一次 shell 脚本 的方法
Linux crontab 每5秒钟执行一次 shell 脚本 的方法 由于 Linux 的 crontab 的定时命令格式如下: minute hour day-of-month month-of- ...
- Debian10服务器安装
对于使用惯windows系统的人来说,刚开始接触使用linux系统一定是很不习惯,因为使用环境的变化经常会出现一些错误.当然,对于我来说,我也是刚刚才开始接触Linux,对此,有些地方想不到的,可以多 ...
- JavaScript基础入门09
目录 JavaScript 基础入门09 Event 自定义右键菜单 获取鼠标按键 获取鼠标坐标 获取键盘按键 页面中位置的获取 浏览器的默认行为 冒泡 什么是冒泡 小练习 JavaScript 基础 ...
- tyan 3+1+15 远程
直接修改好BIOS,输入IP即可访问,帐号anonymous 这个登录 也就是为空,直接写入密码,登录即可
- selenium 安装流程
安装 1.安装python 2.cmd中输入:pip install selenium 输入pip show selenium检查是否安装完成 3.下载chromedriver 在浏览器中输入chro ...
- web端 微软 RDLC 报表插件 宽大于高 横向打印失效 解决方案
起因于系统报表工具使用的RDLC,本地测试一直使用的纵向打印,未测试过横向打印
- Shell初学(六)Linux Shell 时间运算以及时间差计算方法
Linux Shell 时间运算以及时间差计算方法 时间的加减,以及时间差的计算. 1. 时间加减 这里处理方法,是将基础的时间转变为时间戳,然后,需要增加或者改变时间,变成 秒. 如:1990-01 ...
- Java switch case 语句
switch case 语句判断一个变量与一系列值中某个值是否相等,每个值称为一个分支. 语法 switch(expression){ case value : //语句 break; //可选 ca ...
- IDEA2018.2.6激活(可用)
破解插件下载: 链接:https://pan.baidu.com/s/1j2_kEm_Akcph6Qb8hr6soQ 提取码:hv64 将下载包放入bin文件夹下,修改bin中的两个文件 idea.e ...
- Django基础之模型层(下)
聚合查询 关键字:aggregate from django.db.models import Max,Min,Sum,Count,Avg 统计所有书的平均价格 models.Book.objects ...