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

原文地址为: Java后台发送Post请求,数据传输格式JSON

代码如下:

 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 %>

 

所使用的 jar包:

commons-beanutils-1.8.3.jar

commons-codec-1.4.jar

commons-collections-3.2.1.jar

commons-lang-2.5.jar

commons-lang3-3.1.jar

commons-logging-1.1.1.jar

ezmorph-1.0.6.jar

httpclient-4.1.2.jar

httpcore-4.1-alpha1.jar

json-lib-2.4-jdk15.jar

morph-1.1.1.jar

morph-sandbox-1.1.1.jar

 


转载请注明本文地址: Java后台发送Post请求,数据传输格式JSON

你可能感兴趣的:(Java后台发送Post请求,数据传输格式JSON)