代碼如下:
import java.io.BufferedReader; import java.io.DataOutputStream; import java.io.InputStreamReader; import java.net.HttpURLConnection; import java.net.URL; import java.net.URLEncoder; public class DemoTest1 { public static final String GET_URL = "http://112.4.27.9/mall-back/if_user/store_list?storeId=32"; // public static final String POST_URL = "http://112.4.27.9/mall-back/if_user/store_list"; // 妙兜測(cè)試接口 public static final String POST_URL = "http://121.40.204.191:8180/mdserver/service/installLock"; /** * 接口調(diào)用 GET */ public static void httpURLConectionGET() { try { URL url = new URL(GET_URL); // 把字符串轉(zhuǎn)換為URL請(qǐng)求地址 HttpURLConnection connection = (HttpURLConnection) url.openConnection();// 打開(kāi)連接 connection.connect();// 連接會(huì)話 // 獲取輸入流 BufferedReader br = new BufferedReader(new InputStreamReader(connection.getInputStream(), "UTF-8")); String line; StringBuilder sb = new StringBuilder(); while ((line = br.readLine()) != null) {// 循環(huán)讀取流 sb.append(line); } br.close();// 關(guān)閉流 connection.disconnect();// 斷開(kāi)連接 System.out.PRintln(sb.toString()); } catch (Exception e) { e.printStackTrace(); System.out.println("失敗!"); } } /** * 接口調(diào)用 POST */ public static void httpURLConnectionPOST () { try { URL url = new URL(POST_URL); // 將url 以 open方法返回的urlConnection 連接強(qiáng)轉(zhuǎn)為HttpURLConnection連接 (標(biāo)識(shí)一個(gè)url所引用的遠(yuǎn)程對(duì)象連接) HttpURLConnection connection = (HttpURLConnection) url.openConnection();// 此時(shí)cnnection只是為一個(gè)連接對(duì)象,待連接中 // 設(shè)置連接輸出流為true,默認(rèn)false (post 請(qǐng)求是以流的方式隱式的傳遞參數(shù)) connection.setDoOutput(true); // 設(shè)置連接輸入流為true connection.setDoInput(true); // 設(shè)置請(qǐng)求方式為post connection.setRequestMethod("POST"); // post請(qǐng)求緩存設(shè)為false connection.setUseCaches(false); // 設(shè)置該HttpURLConnection實(shí)例是否自動(dòng)執(zhí)行重定向 connection.setInstanceFollowRedirects(true); // 設(shè)置請(qǐng)求頭里面的各個(gè)屬性 (以下為設(shè)置內(nèi)容的類(lèi)型,設(shè)置為經(jīng)過(guò)urlEncoded編碼過(guò)的from參數(shù)) // application/x-javascript text/xml->xml數(shù)據(jù) application/x-Javascript->json對(duì)象 application/x-www-form-urlencoded->表單數(shù)據(jù) // ;charset=utf-8 必須要,不然妙兜那邊會(huì)出現(xiàn)亂碼【★★★★★】 connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded;charset=utf-8"); // 建立連接 (請(qǐng)求未開(kāi)始,直到connection.getInputStream()方法調(diào)用時(shí)才發(fā)起,以上各個(gè)參數(shù)設(shè)置需在此方法之前進(jìn)行) connection.connect(); // 創(chuàng)建輸入輸出流,用于往連接里面輸出攜帶的參數(shù),(輸出內(nèi)容為?后面的內(nèi)容) DataOutputStream dataout = new DataOutputStream(connection.getOutputStream()); String app_key = "app_key="+ URLEncoder.encode("4f7bf8c8260124e6e9c6bf094951a111", "utf-8"); // 已修改【改為錯(cuò)誤數(shù)據(jù),以免信息泄露】 String agt_num = "&agt_num="+ URLEncoder.encode("10111", "utf-8"); // 已修改【改為錯(cuò)誤數(shù)據(jù),以免信息泄露】 String pid = "&pid="+ URLEncoder.encode("BLZXA150401111", "utf-8"); // 已修改【改為錯(cuò)誤數(shù)據(jù),以免信息泄露】 String departid = "&departid="+ URLEncoder.encode("10007111", "utf-8"); // 已修改【改為錯(cuò)誤數(shù)據(jù),以免信息泄露】 String install_lock_name = "&install_lock_name="+ URLEncoder.encode("南天大門(mén)", "utf-8"); String install_address = "&install_address="+ URLEncoder.encode("北京育新", "utf-8"); String install_gps = "&install_gps="+ URLEncoder.encode("116.350888,40.011001", "utf-8"); String install_work = "&install_work="+ URLEncoder.encode("小李", "utf-8"); String install_telete = "&install_telete="+ URLEncoder.encode("13000000000", "utf-8"); String intall_comm = "&intall_comm="+ URLEncoder.encode("一切正常", "utf-8"); // 格式 parm = aaa=111&bbb=222&ccc=333&ddd=444 String parm = app_key+ agt_num+ pid+ departid+ install_lock_name+ install_address+ install_gps+ install_work+ install_telete+ intall_comm; // 將參數(shù)輸出到連接 dataout.writeBytes(parm); // 輸出完成后刷新并關(guān)閉流 dataout.flush(); dataout.close(); // 重要且易忽略步驟 (關(guān)閉流,切記!) // System.out.println(connection.getResponseCode()); // 連接發(fā)起請(qǐng)求,處理服務(wù)器響應(yīng) (從連接獲取到輸入流并包裝為bufferedReader) BufferedReader bf = new BufferedReader(new InputStreamReader(connection.getInputStream(), "UTF-8")); String line; StringBuilder sb = new StringBuilder(); // 用來(lái)存儲(chǔ)響應(yīng)數(shù)據(jù) // 循環(huán)讀取流,若不到結(jié)尾處 while ((line = bf.readLine()) != null) { // sb.append(bf.readLine()); sb.append(line).append(System.getProperty("line.separator")); } bf.close(); // 重要且易忽略步驟 (關(guān)閉流,切記!) connection.disconnect(); // 銷(xiāo)毀連接 System.out.println(sb.toString()); } catch (Exception e) { e.printStackTrace(); } } public static void main(String[] args) { // httpURLConectionGET(); httpURLConnectionPOST(); } }只使用了POST請(qǐng)求方法,GET沒(méi)有用,為了保證代碼完整性所以沒(méi)有刪除GET請(qǐng)求代碼 返回結(jié)果:
{ "status" : "fail", "code" : "ERR001", "msg" : "商戶(hù)10111不存在" }
新聞熱點(diǎn)
疑難解答
圖片精選
網(wǎng)友關(guān)注