第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定

post發(fā)送json請求,返回inputstream(用于獲取微信小程序二維碼)

標簽:
Java

1,post发送json请求,返回inputstream(用于获取微信小程序二维码)

 public static InputStream jsonPostToStream(String url, Map<String, Object> param) {
        if (StringUtils.isEmpty(url)) {
            throw new IllegalArgumentException("url is required");
        }
        CloseableHttpClient httpClient = HttpClients.createDefault();
        HttpPost httpPost = new HttpPost(url);

        for (Map.Entry<String, String> entry : getHeaders().entrySet()) {
            httpPost.setHeader(entry.getKey(), entry.getValue());
        }
        httpPost.setHeader("Accept", "application/json");
        httpPost.setHeader("Content-Type", "application/json");
        StringEntity entity = new StringEntity(JSON.toJSONString(param), "UTF-8");

        httpPost.setEntity(entity);

        CloseableHttpResponse response = null;
        try {
            response = httpClient.execute(httpPost);
            if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
                return response.getEntity().getContent();
            } else {
                LOGGER.info("response error! error code {}", response.getStatusLine().getStatusCode());
            }
        } catch (IOException e) {
            LOGGER.info("receive post response error! url {}", url, e);
            try {
                response.close();
                httpClient.close();
            } catch (IOException e1) {
                LOGGER.info("execute post req error!", e1);
            }
        }
        return null;
    }

2,普通get请求

 /**
     * 发送get请求
     *
     * @param url 请求连接
     * @return t
     */
    public static String doGet(String url) {
        LOGGER.error("Get请求url{}:", url);
        if (StringUtils.isEmpty(url)) {
            throw new IllegalArgumentException("url is required");
        }

        CloseableHttpClient httpClient = HttpClients.createDefault();
        HttpGet httpGet = new HttpGet(url);
        for (Map.Entry<String, String> entry : getHeaders().entrySet()) {
            httpGet.setHeader(entry.getKey(), entry.getValue());
        }

        //发送请求
        try {
            CloseableHttpResponse response = httpClient.execute(httpGet);
            if (response.getStatusLine().getStatusCode() != HttpStatus.SC_OK) {
                return null;
            }
            HttpEntity entity = response.getEntity();
            String res = EntityUtils.toString(entity);
            EntityUtils.consume(entity);

            //关闭响应
            response.close();
            return res;
        } catch (IOException e) {
            LOGGER.error("GET请求{}失败, errorStack:", url, e);
        } finally {
            try {
                httpClient.close();
            } catch (IOException e) {
                LOGGER.error("关闭GET请求Client失败, errorStack:", e);
            }
        }
        return null;
    }

3,发送form-data 表单请求/发送json请求(通过参数isJson进行区分)

 /**
     * 发送post请求
     *
     * @param url   请求的url
     * @param param 参数列表
     * @param <T>   返回值类型
     * @return t
     */
    private static <T> T doPost(String url, Map<String, Object> param, Boolean isJson, Class<T> clazz) {
        if (StringUtils.isEmpty(url)) {
            throw new IllegalArgumentException("url is required");
        }
        CloseableHttpClient httpClient = HttpClients.createDefault();
        HttpPost httpPost = new HttpPost(url);

        for (Map.Entry<String, String> entry : getHeaders().entrySet()) {
            httpPost.setHeader(entry.getKey(), entry.getValue());
        }

        if (isJson) {
            httpPost.setHeader("Accept", "application/json");
            httpPost.setHeader("Content-Type", "application/json");
            StringEntity entity = new StringEntity(JSON.toJSONString(param), "UTF-8");

            httpPost.setEntity(entity);
        } else {
            List<NameValuePair> form = new ArrayList<>();
            for (Iterator iterator = param.entrySet().iterator(); iterator.hasNext(); ) {
                String name = String.valueOf(iterator.next());
                String value = String.valueOf(param.get(name));
                form.add(new BasicNameValuePair(name, value));
            }

            try {
                httpPost.setEntity(new UrlEncodedFormEntity(form, "UTF-8"));
            } catch (UnsupportedEncodingException e) {
                LOGGER.info("form post error!", e);
                return null;
            }
        }

        CloseableHttpResponse response = null;
        try {
            response = httpClient.execute(httpPost);
            if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
                if (isJson) {
                    return JSON.parseObject(EntityUtils.toString(response.getEntity()), clazz);
                }
                return (T) EntityUtils.toString(response.getEntity());
            } else {
                LOGGER.info("response error! error code {}", response.getStatusLine().getStatusCode());
            }
        } catch (IOException e) {
            LOGGER.info("receive post response error! url {}", url, e);
            try {
                response.close();
                httpClient.close();
            } catch (IOException e1) {
                LOGGER.info("execute post req error!", e1);
            }
        }
        return null;
    }
點擊查看更多內(nèi)容
TA 點贊

若覺得本文不錯,就分享一下吧!

評論

作者其他優(yōu)質(zhì)文章

正在加載中
JAVA開發(fā)工程師
手記
粉絲
11
獲贊與收藏
108

關(guān)注作者,訂閱最新文章

閱讀免費教程

  • 推薦
  • 評論
  • 收藏
  • 共同學習,寫下你的評論
感謝您的支持,我會繼續(xù)努力的~
掃碼打賞,你說多少就多少
贊賞金額會直接到老師賬戶
支付方式
打開微信掃一掃,即可進行掃碼打賞哦
今天注冊有機會得

100積分直接送

付費專欄免費學

大額優(yōu)惠券免費領(lǐng)

立即參與 放棄機會
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學習伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號

舉報

0/150
提交
取消