JAVA POST請(qǐng)求

/**
     * * 向指定 URL 發(fā)送POST方法的請(qǐng)求 * * @param url 發(fā)送請(qǐng)求的 URL * @param param 請(qǐng)求參數(shù),請(qǐng)求參數(shù)應(yīng)該是
     * name1=value1&name2=value2 的形式。 * @return 所代表遠(yuǎn)程資源的響應(yīng)結(jié)果
     */
    public static String sendPost(String url, String param, Map<String, String> headMap) {
        logger.info("請(qǐng)求地址:" + url);
        logger.info("請(qǐng)求參數(shù):" + param);
        OutputStreamWriter out = null;
        BufferedReader in = null;
        String result = "";
        if (StringUtils.isNotBlank(url)) {
            try {
                URL realUrl = new URL(url);
                // 打開和URL之間的連接
                URLConnection connection = realUrl.openConnection();
                // 設(shè)置通用的請(qǐng)求屬性
                connection.setRequestProperty("accept", "*/*");
                connection.setRequestProperty("connection", "Keep-Alive");
                connection.setRequestProperty("Content-Type", "application/json;charset=utf-8");
                if (null != headMap && headMap.size() > 0) {
                    for (Map.Entry<String, String> entry : headMap.entrySet()) {
                        connection.setRequestProperty(entry.getKey(), entry.getValue());
                    }
                }
                // 發(fā)送POST請(qǐng)求必須設(shè)置如下兩行
                connection.setDoOutput(true);
                connection.setDoInput(true);
                // 獲取URLConnection對(duì)象對(duì)應(yīng)的輸出流
                out = new OutputStreamWriter(connection.getOutputStream(), "utf-8");
                // 發(fā)送請(qǐng)求參數(shù)
                if (null != param) {
                    out.write(param);
                }
                // flush輸出流的緩沖
                out.flush();
                // 定義BufferedReader輸入流來讀取URL的響應(yīng)
                in = new BufferedReader(new InputStreamReader(connection.getInputStream(), "utf-8"));
                String line;
                while ((line = in.readLine()) != null) {
                    result += line;
                }
            } catch (Exception e) {
                logger.info("HTTP_LOG_INFO:" + e);
            } finally {
                // 使用finally塊來關(guān)閉輸出流、輸入流
                try {
                    if (in != null) {
                        in.close();
                    }
                    if (out != null) {
                        out.close();
                    }
                } catch (IOException ex) {
                    logger.info("HTTP_LOG_INFO:" + ex);
                }
            }
        }
        logger.info("post請(qǐng)求返回值:" + result);
        return result;
    }
// 訪問post 傳值
Map<String, String> headerParameters = new HashMap<String, String>(4);
// 此處為旗縣端6位行政區(qū)代碼
headerParameters.put("dwdm", "123456");
String data = "123";
logger.info("請(qǐng)求信息:" + data);
// 此處傳省廳接管接口地址  url是請(qǐng)求地址,data是請(qǐng)求參數(shù),headerParameters是請(qǐng)求頭
String result = sendPost(url, data, headerParameters);
logger.info("返回信息:" + result);
if (StringUtils.isNotEmpty(result)) {
// 此處使用旗縣端登記系統(tǒng)私鑰進(jìn)行解密
logger.info("返回信息:" + result);
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

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

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