HttpClient+json发送和接收参数——基于struts2或者ssh等框架
来源:互联网 发布:linux shell 小数运算 编辑:程序博客网 时间:2024/06/06 10:10
HttpClient+json发送和接收参数——基于struts2或者ssh等框架
json操作需要的jar包:http://blog.csdn.net/bestcxx/article/details/49977333
发送方:
//模拟-查询支付情况-请求的方法test1():
public void test1(){
try {
//创建连接
HttpClient httpclient = new DefaultHttpClient();
// 设置Cookie的兼容性
httpclient.getParams().setParameter(ClientPNames.COOKIE_POLICY, CookiePolicy.BROWSER_COMPATIBILITY);
httpclient.getParams().setParameter(CoreProtocolPNames.PROTOCOL_VERSION, HttpVersion.HTTP_1_1);
httpclient.getParams().setParameter("http.protocol.content-charset","utf-8");
httpclient.getParams().setParameter(HTTP.CONTENT_ENCODING, "utf-8");
httpclient.getParams().setParameter(HTTP.CHARSET_PARAM, "utf-8");
httpclient.getParams().setParameter(HTTP.DEFAULT_PROTOCOL_CHARSET,"utf-8");
// 15秒连接不上则失败
httpclient.getConnectionManager().closeIdleConnections(15, TimeUnit.SECONDS);
HttpPost httppost = new HttpPost("http://***/test2.do"); //这里路径设置为你自己的,比如127.0.0.1:8080/项目名/请求.do
//创建JSON类型对象
JSONObject json1=new JSONObject();
//向JSON对象中放入json类型的数据
json1.put("name", "李明1");
json1.put("age", "10");
HttpResponse response;
StringEntity entity = new StringEntity(json1.toString(),"utf-8");//解决中文乱码问题
entity.setContentEncoding("UTF-8");
entity.setContentType("application/json");
httppost.setEntity(entity);
response = httpclient.execute(httppost);
String result=EntityUtils.toString(response.getEntity(),"utf8");
result = StringUtils.isNotBlank(result) ? result : "noSuccess";
System.out.println(result); //显示返回的json串
JSONObject jsonResult=JSONObject.fromObject(result);
System.out.println(jsonResult.get("name")); //获取json中的参数
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
} catch (IOException e) {
// TODO Auto-generated catch block
}
}
//接收方:
//模拟-处理支付情况-处理的方法-test2()
public void checkPayContent(){
ActionContext ctx = ActionContext.getContext();
HttpServletRequest request = (HttpServletRequest)ctx.get(ServletActionContext.HTTP_REQUEST);
HttpServletResponse response = (HttpServletResponse)ctx.get(ServletActionContext.HTTP_RESPONSE);
InputStream inputStream;
try {
inputStream = request.getInputStream();
String strMessage = "";
String strResponse = "";
BufferedReader reader;
reader = new BufferedReader(new InputStreamReader(inputStream,"utf-8"));
while ((strMessage = reader.readLine()) != null) {
strResponse += strMessage;
}
reader.close();
inputStream.close();
System.out.println("strResponse=" + strResponse);
JSONObject jsonPayRequest = JSONObject.fromObject(strResponse);
//获取json中参数
String name=jsonPayRequest.getString("name");
System.out.println("null,结束的xml");
System.out.println("jsonPayRequest.toString()=" + jsonPayRequest.toString());
response.setCharacterEncoding("UTF-8");// 解决发起端接受回复的中文乱码问题
PrintWriter printWriter = response.getWriter();
printWriter.write(jsonPayRequest.toString());// 返回的xml报文
printWriter.flush();
printWriter.close();
} catch (IOException e) {
// TODO Auto-generated catch block
}
}
- HttpClient+json发送和接收参数——基于struts2或者ssh等框架
- HttpClient+List<NameValuePair>发送和接收参数——基于struts2或者ssh等框架
- HttpClient和HttpUrlConnection实现post发送和接收json数据
- 简单的使用httpclient调接口发送和接收JSon
- SSH框架学习之Struts2(2):接收参数
- HttpClient模拟get,post请求并发送请求参数(json等)
- HttpClient模拟get,post请求并发送请求参数(json等)
- HttpClient模拟get,post请求并发送请求参数(json等)
- httpClient发送key_value、json参数及文件
- json的发送和接收
- 【05】SSH练习——struts2和json 的整合
- SSH与SSM学习之Struts209——OGNL表达式与Struts框架结合参数接收
- ssh框架使用json和ajax传递参数的区别
- SpringMVC 通过post接收form参数或者json参数
- 毕设系列—服务端:Java-SSH框架(2)之服务器端发送JSON格式数据
- httpPost对JSON发送和接收
- 前后端json数据发送和接收
- struts2发送json数组,js使用ajax接收。
- x-requested-with 请求头 区分ajax请求还是普通请求
- scroller
- LBP方法
- 关于视频播放和下载开发的一些总结
- 前景检测算法_3(GMM)
- HttpClient+json发送和接收参数——基于struts2或者ssh等框架
- 运维工程师必备之MySQL数据的主从复制、半同步复制和主主复制详解
- RPG Maker MV 游戏开发新选择
- Android AutoLayout全新的适配方式 堪称适配终结者
- POJ 2420 A Star not a Tree? (爬山法||模拟退火)
- JQuery按照form上传文件+asp.net后台处理(三)
- Selenium2(webdirver)入门之四常见问题(Java版)
- 关于Android的.so文件你所需要知道的
- context