天天看點

第三方接口怎麼寫

如何對第三方接口發送url請求

好的,今天我們來寫一個,向第三方接口發送url請求。

想必我們平時也經常會遇到向第三方接口發送請求,有時有個對方提供的jar包方法友善些,填參數就行,但偶爾遇到某些參數寫在了jar包裡無法修改,而我們又必須要修改的時候就需要自己發送url請求過去。

或者活得苦一點,第三方幹脆就給了個url連結跟要填的參數/(ㄒoㄒ)/~~

或者更過分一點,遇到老項目,傳的還是xml格式

那看看我的方法,按需替換,增加一下字段就可以使用了

//方法名随便取一個
public String 方法名(String serviceId){
		//這個就是url位址,防止被人攻擊,随便寫一個,自己替換一下
		String addFirst = "http://11.1.11.111:1111/xxxxxx/xxxxxx?xxxxxx=&xxxxxx=&xxxxxx=xxxx";
        HttpURLConnection con = null;
        //要傳過去的xml,别照抄啊,寫你要的xml,菜鳥裡有xml格式驗證附上連結https://www.runoob.com/xml/xml-validator.html,不知道怎麼寫問第三方要傳的内容,組一組就行了,要用json的換成json的資料就行了,防止洩露,我删點xml内容,沒驗證,可能格式錯誤了,自己替換一下
        String requestBodyFirst = "<soapenv:Envelope xmlns:soapenv=\"http://xxxxxxxxxx\">" +
                "   <soapenv:Header/>" +
                "   <soapenv:Body>" +
                "      <urn:MT_ZHLH_DZJ_ECC_ORDER_DETAIL_REQ>" +
                //要填進去内容可以像下面這樣寫
                "      <IV_NUMBER>"+serviceId+"</IV_NUMBER></urn:MT_ZHLH_DZJ_ECC_ORDER_DETAIL_REQ>" +
                "   </soapenv:Body>" +
                "</soapenv:Envelope>";
        HttpURLConnection con = null;
        try {
        	//下面可以照抄
        	// 指定WebService服務的請求位址:
			// 建立URL:
            URL url = new URL(addFirst);
            // 建立連接配接,并将連接配接強轉為Http連接配接
            URLConnection conn = url.openConnection();
            con = (HttpURLConnection) conn;
            // 設定請求方式和請求頭:
            con.setDoInput(true); // 是否有入參
            con.setDoOutput(true); // 是否有出參
            con.setRequestMethod("POST"); // 設定請求方式
            con.setRequestProperty("content-type", "text/xml;charset=UTF-8");
            //通過流的方式将請求體發送出去
            OutputStream out = con.getOutputStream();
            out.write(xml.getBytes());
            out.close();
            int code = con.getResponseCode();
            //服務端傳回正常
            if (code == 200) {
                InputStream is = con.getInputStream();
                byte[] b = new byte[1024];
                StringBuffer sb = new StringBuffer();
                int len = 0;
                while ((len = is.read(b)) != -1) {
                    String str = new String(b, 0, len, "UTF-8");
                    sb.append(str);
                }
                //回參xml轉string
                String xmlReturn = sb.toString();
                is.close();
                //好了,抄到這,下面根據不同三方接口的回參,因為我是xml回參,我就截取文字,我這先截取了一個大的資料,然後再擷取裡面的type
                String type = xmlReturn.substring(xmlReturn.indexOf("<xml>") + 11, xmlReturn.indexOf("</xml>"));
                type = type.substring(type.indexOf("<type>") + 6, type.indexOf("</type>"));
                is.close();
                //我這type=S代表成功查出來資料了
                if (type.equals("S")) {
                	//截取body裡所有資料回傳到别的接口
                    re = xmlReturn.substring(xmlReturn.indexOf("<xmlbody>") + 11, xmlReturn.indexOf("</xmlbody>"));
                    return re;
                } else {
                    return re = "false";
                }
            } else {
                return re = "false";
            }
        }catch (Exception e) {
// TODO: handle exception
            e.printStackTrace();
        } finally {
            if (con != null) {
                con.disconnect();
            }
        }
        return re;
    }
           

好了,這個時候我們獲得了回參裡的所有主要資料,是不是還想知道知道怎麼拿出裡面的資料,我知道你懶得思考,上面方法裡的截取字段可以直接用,比如你要擷取回參xml裡的ACCCC字段,用這句話

這個時候擷取出來的ACCCC就是要的資料

ok今天的講解結束了