java調介面

來源:互聯網
上載者:User

標籤:pac   ati   傳遞   install   str   stringbu   app   dep   use   

package util;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 interfacetest {//    public static final String POST_URL = "http://112.4.27.9/mall-back/if_user/store_list";// 妙兜測試介面//public static final String POST_URL = "http://121.40.204.191:8180/mdserver/service/installLock";        /**     * 介面調用 GET     */    public static void httpURLConectionGET(String GET_URL) {        try {            URL url = new URL(GET_URL);    // 把字串轉換為URL請求地址            HttpURLConnection connection = (HttpURLConnection) url.openConnection();// 開啟串連            connection.connect();// 串連會話            // 擷取輸入資料流            //GBK為編碼格式,可以修改            BufferedReader br = new BufferedReader(new InputStreamReader(connection.getInputStream(), "GBK"));            String line;            StringBuilder sb = new StringBuilder();            while ((line = br.readLine()) != null) {// 迴圈讀取流                sb.append(line);            }            br.close();// 關閉流            connection.disconnect();// 中斷連線            System.out.println(sb.toString());            //System.out.println(new String(sb.toString().getBytes()));        } catch (Exception e) {            e.printStackTrace();            System.out.println("失敗!");        }    }        /**     * 介面調用  POST     */    public static void httpURLConnectionPOST (String POST_URL) {        try {            URL url = new URL(POST_URL);                        // 將url 以 open方法返回的urlConnection  串連強轉為HttpURLConnection串連  (標識一個url所引用的遠程對象串連)            HttpURLConnection connection = (HttpURLConnection) url.openConnection();// 此時cnnection只是為一個連線物件,待串連中                        // 設定串連輸出資料流為true,預設false (post 請求是以流的方式隱式的傳遞參數)            connection.setDoOutput(true);                        // 設定串連輸入資料流為true            connection.setDoInput(true);                        // 佈建要求方式為post            connection.setRequestMethod("POST");                        // post請求緩衝設為false            connection.setUseCaches(false);                        // 設定該HttpURLConnection執行個體是否自動執行重新導向            connection.setInstanceFollowRedirects(true);                        // 佈建要求頭裡面的各個屬性 (以下為設定內容的類型,設定為經過urlEncoded編碼過的from參數)            // application/x-javascript text/xml->xml資料 application/x-javascript->json對象 application/x-www-form-urlencoded->表單資料            // ;charset=utf-8 必須要,不然妙兜那邊會出現亂碼【★★★★★】            connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded;charset=utf-8");                        // 建立串連 (請求未開始,直到connection.getInputStream()方法調用時才發起,以上各個參數設定需在此方法之前進行)            connection.connect();                        // 建立輸入輸出資料流,用於往串連裡面輸出攜帶的參數,(輸出內容為?後面的內容)            DataOutputStream dataout = new DataOutputStream(connection.getOutputStream());                        String app_key = "app_key="+ URLEncoder.encode("4f7bf8c8260124e6e9c6bf094951a111", "utf-8");// 已修改【改為錯誤資料,以免資訊泄露】            String agt_num = "&agt_num="+ URLEncoder.encode("10111", "utf-8");// 已修改【改為錯誤資料,以免資訊泄露】            String pid = "&pid="+ URLEncoder.encode("BLZXA150401111", "utf-8");// 已修改【改為錯誤資料,以免資訊泄露】            String departid = "&departid="+ URLEncoder.encode("10007111", "utf-8");// 已修改【改為錯誤資料,以免資訊泄露】            String install_lock_name = "&install_lock_name="+ URLEncoder.encode("南天大門", "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;                        // 將參數輸出到串連            dataout.writeBytes(parm);                        // 輸出完成後重新整理並關閉流            dataout.flush();            dataout.close(); // 重要且易忽略步驟 (關閉流,切記!)             //            System.out.println(connection.getResponseCode());                        // 串連發起請求,處理伺服器響應  (從串連擷取到輸入資料流並封裝為bufferedReader)            BufferedReader bf = new BufferedReader(new InputStreamReader(connection.getInputStream(), "UTF-8"));             String line;            StringBuilder sb = new StringBuilder(); // 用來儲存響應資料                        // 迴圈讀取流,若不到結尾處            while ((line = bf.readLine()) != null) {//                sb.append(bf.readLine());            sb.append(line).append(System.getProperty("line.separator"));            }            bf.close();    // 重要且易忽略步驟 (關閉流,切記!)             connection.disconnect(); // 銷毀串連            System.out.println(sb.toString());            } catch (Exception e) {            e.printStackTrace();        }    }        public static void main(String[] args) {//        httpURLConectionPOST("http://121.40.204.191:8180/mdserver/service/installLock");    httpURLConectionGET("https://sp0.baidu.com/5a1Fazu8AA54nxGko9WTAnF6hhy/su?wd=123&json=1&p=3&sid=1438_21098_17001_22159&req=2&pbs=12306&csor=3&pwd=12&cb=jQuery110208408865132022476_1508200165647&_=1508200165650");        }}

  

java調介面

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.