java/SpringBoot http post請求傳json資料格式(有詳細註解)
阿新 • • 發佈:2019-01-24
java http post請求傳json資料格式,試了很多的方式,用了下面這個是可以 import net.sf.json.JSONObject; 2 import org.apache.commons.httpclient.*; 3 import org.apache.commons.httpclient.methods.GetMethod; 4 import org.apache.commons.httpclient.params.HttpMethodParams; 5 import org.apache.http.HttpEntity; 6 import org.apache.http.HttpResponse; 7 import org.apache.http.client.methods.HttpPost; 8 import org.apache.http.entity.StringEntity; 9 import org.apache.http.impl.client.DefaultHttpClient; 10 import org.apache.http.util.EntityUtils; 11 import java.io.IOException; 12 13 /** 14 * Created by liqun.chen on 2017/5/15. 15 */ 16 public class HttpUtil { 17 /** 18 * json 字串 19 * @param url 20 * @param param 21 * @return 22 */ 23 public static String getSerchPersion(String url,String param){ 24 /* 1 生成 HttpClinet 物件並設定引數 */ 25 HttpClient httpClient = new HttpClient(); 26 // 設定 Http 連線超時為5秒 27 httpClient.getHttpConnectionManager().getParams().setConnectionTimeout(5000); 28 /* 2 生成 GetMethod 物件並設定引數 */ 29 GetMethod getMethod = new GetMethod(url); 30 // 設定 get 請求超時為 5 秒 31 getMethod.getParams().setParameter(HttpMethodParams.SO_TIMEOUT, 5000); 32 // 設定請求重試處理,用的是預設的重試處理:請求三次 33 getMethod.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, new DefaultHttpMethodRetryHandler()); 34 String response = ""; 35 /* 3 執行 HTTP GET 請求 */ 36 try { 37 int statusCode = httpClient.executeMethod(getMethod); 38 /* 4 判斷訪問的狀態碼 */ 39 if (statusCode != HttpStatus.SC_OK) { 40 System.err.println("請求出錯: "+ getMethod.getStatusLine()); 41 } 42 /* 5 處理 HTTP 響應內容 */ 43 // HTTP響應頭部資訊,這裡簡單列印 44 Header[] headers = getMethod.getResponseHeaders(); 45 for (Header h : headers) 46 System.out.println(h.getName() + "------------ " + h.getValue()); 47 // 讀取 HTTP 響應內容,這裡簡單列印網頁內容 48 byte[] responseBody = getMethod.getResponseBody();// 讀取為位元組陣列 49 response = new String(responseBody, param); 50 System.out.println("----------response:" + response); 51 // 讀取為 InputStream,在網頁內容資料量大時候推薦使用 52 // InputStream response = getMethod.getResponseBodyAsStream(); 53 } catch (HttpException e) { 54 // 發生致命的異常,可能是協議不對或者返回的內容有問題 55 System.out.println("請檢查輸入的URL!"); 56 e.printStackTrace(); 57 } catch (IOException e) { 58 // 發生網路異常 59 System.out.println("發生網路異常!"); 60 e.printStackTrace(); 61 } finally { 62 /* 6 .釋放連線 */ 63 getMethod.releaseConnection(); 64 } 65 return response; 66 } 67 /** 68 * post請求 69 * @param url 70 * @param json 71 * @return 72 */ 73 public static JSONObject doPost(String url,JSONObject json){ 74 DefaultHttpClient client = new DefaultHttpClient(); 75 HttpPost post = new HttpPost(url); 76 JSONObject response = null; 77 try { 78 StringEntity s = new StringEntity(json.toString()); 79 s.setContentEncoding("UTF-8"); 80 s.setContentType("application/json");//傳送json資料需要設定contentType 81 post.setEntity(s); 82 HttpResponse res = client.execute(post); 83 if(res.getStatusLine().getStatusCode() == HttpStatus.SC_OK){ 84 HttpEntity entity = res.getEntity(); 85 String result = EntityUtils.toString(res.getEntity());// 返回json格式: 86 response = JSONObject.fromObject(result); 87 } 88 } catch (Exception e) { 89 throw new RuntimeException(e); 90 } 91 return response; 92 } 1 //呼叫 2 public static void main(String arg[]) throws Exception { 3 String url = "http://localhost:8080/"; 4 JSONObject params = new JSONObject(); 5 params.put("personName", "name"); 6 params.put("personCode", "230882xxxxxx2116"); 7 JSONObject param2 = new JSONObject(); 8 param2.put("pageNo", 1); 9 param2.put("pageSize", 20); 10 params.put("page", param2); 11 String param = "q="+params.toString(); 12 //get 請求 13 String ret = getSerchPersion(url, param.toString()); 14 System.out.println(ret); 15 // JSONObject jsonResponse=JSONObject.fromObject(param); 16 // JSONObject json = (JSONObject)jsonResponse.get("page"); 17 // System.out.println(json.get("pageSize")); 18 19 //post 請求 20 JSONObject jsonObject = doPost(url,params); 21 System.out.println(jsonObject.toString()); 22 } 轉載來自:http://www.cnblogs.com/Mr-Chen-blog/articles/6951688.html