這篇文章給大家介紹java中HttpsPost和CloseableHttpClient如何使用,內(nèi)容非常詳細(xì),感興趣的小伙伴們可以參考借鑒,希望對(duì)大家能有所幫助。
成都做網(wǎng)站、網(wǎng)站建設(shè)介紹好的網(wǎng)站是理念、設(shè)計(jì)和技術(shù)的結(jié)合。創(chuàng)新互聯(lián)擁有的網(wǎng)站設(shè)計(jì)理念、多方位的設(shè)計(jì)風(fēng)格、經(jīng)驗(yàn)豐富的設(shè)計(jì)團(tuán)隊(duì)。提供PC端+手機(jī)端網(wǎng)站建設(shè),用營(yíng)銷思維進(jìn)行網(wǎng)站設(shè)計(jì)、采用先進(jìn)技術(shù)開源代碼、注重用戶體驗(yàn)與SEO基礎(chǔ),將技術(shù)與創(chuàng)意整合到網(wǎng)站之中,以契合客戶的方式做到創(chuàng)意性的視覺(jué)化效果。
//請(qǐng)求json public static String HttpsPost(String url, Mapparam) throws Exception{ String respContent =""; String restServiceURL = url; System.out.println(restServiceURL); CloseableHttpClient client = HttpClients.createDefault(); //設(shè)置傳入?yún)?shù) StringEntity entity = new StringEntity(JSONObject.toJSONString(param), "utf-8"); HttpPost httpGet = new HttpPost(restServiceURL); httpGet.addHeader("Content-Type","application/json;charset=UTF-8"); httpGet.setEntity(entity); HttpResponse resp = client.execute(httpGet); int statusCode = resp.getStatusLine().getStatusCode(); if ( 200==statusCode ) { HttpEntity he = resp.getEntity(); respContent = EntityUtils.toString(he, "utf-8"); }else { log.error("請(qǐng)求第三方異常,狀態(tài)碼{},地址{}",statusCode,url); } return respContent; }
/** * http請(qǐng)求 application/x-www-form-urlencoded */ public static String HttpsPostXw(String url, Mapparam) throws Exception{ String respContent =""; String restServiceURL = url; CloseableHttpClient client = HttpClients.createDefault(); //設(shè)置傳入?yún)?shù) HttpPost httpGet = new HttpPost(restServiceURL); httpGet.setHeader("Content-type", "application/x-www-form-urlencoded"); List nvps = new ArrayList (); if(param!=null){ for (Map.Entry entry : param.entrySet()) { nvps.add(new BasicNameValuePair(entry.getKey(), entry.getValue())); } } //設(shè)置參數(shù)到請(qǐng)求對(duì)象中 httpGet.setEntity(new UrlEncodedFormEntity(nvps, "utf-8")); //裝填參數(shù) if(param!=null){ for (Map.Entry entry : param.entrySet()) { httpGet.setHeader(entry.getKey(),entry.getValue()); } } HttpResponse resp = client.execute(httpGet); int statusCode = resp.getStatusLine().getStatusCode(); if ( 200==statusCode ) { HttpEntity he = resp.getEntity(); respContent = EntityUtils.toString(he, "utf-8"); }else { log.error("請(qǐng)求第三方異常,狀態(tài)碼{},地址{}",statusCode,url); } return respContent; }
關(guān)于java中HttpsPost和CloseableHttpClient如何使用就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,可以學(xué)到更多知識(shí)。如果覺(jué)得文章不錯(cuò),可以把它分享出去讓更多的人看到。