Java 백그라운드에서 Post 요청 전송, 데이터 전송 형식 JSON

16261 단어
원본 주소: 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 + "
"); 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
 
전재는 본문의 주소를 명시하십시오: 자바 백엔드에서 Post 요청을 보내고 데이터 전송 형식인 JSON

좋은 웹페이지 즐겨찾기