码迷,mamicode.com
首页 > 编程语言 > 详细

Java后台发送Post请求,数据传输格式JSON

时间:2015-06-14 18:29:34      阅读:351      评论:0      收藏:0      [点我收藏+]

标签:

代码如下:

 1 package com.test;
 2 
 3 import java.io.BufferedReader;
 4 import java.io.InputStream;
 5 import java.io.InputStreamReader;
 6 
 7 import net.sf.json.JSONObject;
 8 
 9 import org.apache.http.HttpResponse;
10 import org.apache.http.HttpStatus;
11 import org.apache.http.client.HttpClient;
12 import org.apache.http.client.methods.HttpPost;
13 import org.apache.http.entity.StringEntity;
14 import org.apache.http.impl.client.DefaultHttpClient;
15 import org.apache.http.message.BasicHeader;
16 import org.apache.http.protocol.HTTP;
17 
18 public abstract class TestSend {
19 
20     public static String URL = "http://115.28.191.62/web/control";
21 
22     public static void main(String[] args) {
23 
24         JSONObject jsobj1 = new JSONObject();
25         JSONObject jsobj2 = new JSONObject();
26         jsobj2.put("deviceID", "112");
27         jsobj2.put("channel", "channel");
28         jsobj2.put("state", "0");
29         jsobj1.put("item", jsobj2);
30         jsobj1.put("requestCommand", "control");
31 
32         post(jsobj1);
33 
34     }
35 
36     public static String post(JSONObject json) {
37 
38         HttpClient client = new DefaultHttpClient();
39         HttpPost post = new HttpPost(URL);
40         
41         post.setHeader("Content-Type", "application/json");
42         post.addHeader("Authorization", "Basic YWRtaW46");
43         String result = "";
44         
45         try {
46 
47             StringEntity s = new StringEntity(json.toString(), "utf-8");
48             s.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE,
49                     "application/json"));
50             post.setEntity(s);
51 
52             // 发送请求
53             HttpResponse httpResponse = client.execute(post);
54 
55             // 获取响应输入流
56             InputStream inStream = httpResponse.getEntity().getContent();
57             BufferedReader reader = new BufferedReader(new InputStreamReader(
58                     inStream, "utf-8"));
59             StringBuilder strber = new StringBuilder();
60             String line = null;
61             while ((line = reader.readLine()) != null)
62                 strber.append(line + "\n");
63             inStream.close();
64 
65             result = strber.toString();
66             System.out.println(result);
67             
68             if (httpResponse.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
69                 
70                     System.out.println("请求服务器成功,做相应处理");
71                 
72             } else {
73                 
74                 System.out.println("请求服务端失败");
75                 
76             }
77             
78 
79         } catch (Exception e) {
80             System.out.println("请求异常");
81             throw new RuntimeException(e);
82         }
83 
84         return result;
85     }
86 
87 }

 

测试接收

 1 <%@ page language="java" import="java.util.*,java.io.*" pageEncoding="UTF-8"%>
 2 <%
 3 
 4     System.out.println("conenction server success!");
 5 
 6     System.out.println(request.getMethod());
 7 
 8     //后台接收
 9     InputStreamReader reader=new InputStreamReader(request.getInputStream(),"UTF-8");
10     char [] buff=new char[1024];
11     int length=0;
12     while((length=reader.read(buff))!=-1){
13         
14         String x=new String(buff,0,length);
15         
16         System.out.println(x);
17     }
18 
19     
20     //响应
21     out.println("{‘responseCommand‘:‘0‘,‘requestCommand‘:‘control‘}");
22 
23 %>

 

Java后台发送Post请求,数据传输格式JSON

标签:

原文地址:http://www.cnblogs.com/thinkpad/p/4575332.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!