Java後台發送Post請求,資料轉送格式JSON

來源:互聯網
上載者:User

標籤:

代碼如下:

 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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.