maven_4_接口和json

接口

物流接口

快遞接口:

http://www.kuaidi100.com/query?type=快遞公司代號&postid=快遞單號

ps:快遞公司編碼:申通=”shentong” EMS=”ems” 順豐=”shunfeng” 圓通=”yuantong” 中通=”zhongtong” 韻達=”yunda” 天天=”tiantian” 匯通=”huitongkuaidi” 全峰=”quanfengkuaidi” 德邦=”debangwuliu” 宅急送=”zhaijisong”

httpClient

package com.guoyasoft;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;


public class HttpClient {
    public static String callHttpByStream(int timeOut, String urlLink,
            String xml, String encode) throws Exception {
        HttpURLConnection httpurlconnection = null;
        try {
            URL url = null;
            url = new URL(urlLink);
            httpurlconnection = (HttpURLConnection) url.openConnection();
            httpurlconnection.setRequestProperty("Content-type", "text/xml");
            httpurlconnection.setDoOutput(true);
            httpurlconnection.setDoInput(true);
            httpurlconnection.setRequestMethod("POST");
            httpurlconnection.setConnectTimeout(timeOut);
            httpurlconnection.setReadTimeout(timeOut);
            httpurlconnection.connect();
        } catch (Exception e) {
            throw e;
        }
        try {
            String SendData = xml;
            httpurlconnection.getOutputStream()
                    .write(SendData.getBytes(encode));
            httpurlconnection.getOutputStream().flush();
            httpurlconnection.getOutputStream().close();
        } catch (Exception e) {
            e.getStackTrace();
            throw e;
        }
        try {
            String result = "";
            BufferedReader in = new BufferedReader(new InputStreamReader(
                    httpurlconnection.getInputStream(), encode));
            String line;
            while ((line = in.readLine()) != null) {
                result += line;
            }
            in.close();
            return result;
        } catch (Exception e) {
            e.getStackTrace();
            throw e;
        } finally {
            if (httpurlconnection != null)
                httpurlconnection.disconnect();
        }
    }

    public static void main(String[] args) {
        try {
            String httpRequest = "http://www.kuaidi100.com/query?type=shentong&postid=10023232";
            String response = HttpClient.callHttpByStream(3000,
                    "http://www.kuaidi100.com/query?type=shentong&postid=10023232", httpRequest,
                    "UTF-8");
            System.out.println("響應報文:" + response);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

        <dependency>
            <groupId>com.google.code.gson</groupId>
            <artifactId>gson</artifactId>
            <version>2.7</version>
        </dependency>
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容