struts2 接收不到post来的请求参数
struts2 接收不到post来的请求参数(json)字符串,试了两种方式,可以正常接收Get来的参数,但是如果post json字符串就接收不了,如果是json数组可以接受的到
发送方:
/**
* 发起http请求并获取结果
*
* @param requestUrl 请求地址
* @param requestMethod 请求方式(GET、POST)
* @param outputStr 提交的数据
* @return JSONObject(通过JSONObject.get(key)的方式获取json对象的属性值)
*/
public static JSONObject httpRequest(String requestUrl, String requestMethod, String outputStr) {
JSONObject jsonObject = null;
StringBuffer buffer = new StringBuffer();
InputStream inputStream=null;
try {
URL url = new URL(requestUrl);
HttpURLConnection httpUrlConn = (HttpURLConnection) url.openConnection();
httpUrlConn.setDoOutput(true);
httpUrlConn.setDoInput(true);
httpUrlConn.setUseCaches(false);
// 设置请求方式(GET/POST)
httpUrlConn.setRequestMethod(requestMethod);
if ("GET".equalsIgnoreCase(requestMethod))
httpUrlConn.connect();
// 当有数据需要提交时
if (null != outputStr) {
OutputStream outputStream = httpUrlConn.getOutputStream();
// 注意编码格式,防止中文乱码
outputStream.write(outputStr.getBytes("UTF-8"));
outputStream.close();
}
//将返回的输入流转换成字符串
inputStream = httpUrlConn.getInputStream();
InputStreamReader inputStreamReader = new InputStreamReader(inputStream, "utf-8");
BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
String str = null;
while ((str = bufferedReader.readLine()) != null) {
buffer.append(str);
}
bufferedReader.close();
inputStreamReader.close();
// 释放资源
inputStream.close();
inputStream = null;
httpUrlConn.disconnect();
jsonObject = JSONObject.fromObject(buffer.toString());
} catch (ConnectException ce) {
ce.printStackTrace();
System.out.println("Weixin server connection timed out");
} catch (Exception e) {
e.printStackTrace();
System.out.println("http request error:{}");
}finally{
try {
if(inputStream!=null){
inputStream.close();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return jsonObject;
}
接收方:
@SuppressWarnings("serial")
public class FicationAction extends ActionSupport{
private String t;
private String r;
public String getT() {
return t;
}
public void setT(String t) {
this.t = t;
}
public String getR() {
return r;
}
public void setR(String r) {
this.r = r;
}
private static Logger logger =
public void fication() throws Exception{
logger.debug("请求r:\"" + r + "\"");
System.out.println("r" + r);
System.out.println("t" + t);
HttpServletRequest request = ServletActionContext.getRequest();
BufferedReader br = new BufferedReader(new InputStreamReader(request.getInputStream(), "UTF-8"));
StringBuffer sb = new StringBuffer("");
String temp;
while ((temp = br.readLine()) != null) {
sb.append(temp);
}
br.close();
String reqMessage = sb.toString();
System.out.println("请求:\"" + reqMessage + "\"");
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
request能接收到参数,就是读不出来