Java調(diào)用參數(shù)類(lèi)型是application/x-www-form-urlencoded的API問(wèn)題
一、分析
首先用postman測(cè)試對(duì)應(yīng)的接口
測(cè)試如下:
其中請(qǐng)求頭中content-type為application/x-www-form-urlencoded
參數(shù)是:
queryParam:{"id":"mobile","userName":"name","mobile":"12345678"}
注意:
我們進(jìn)行API調(diào)用時(shí),參數(shù)需要轉(zhuǎn)為String類(lèi)型,平時(shí)我們調(diào)用get請(qǐng)求application/x-www-form-urlencoded參數(shù)都是直接拼接在url后面, url?字段名=值&字段名=值,所以這里用同樣的方式進(jìn)行參數(shù)處理。
二、代碼樣例
public String getNameCode(String name,String phone) throws Exception { // 1.組裝數(shù)據(jù),以及請(qǐng)求頭 Map header = new HashMap(); String newParam = "queryParam={\"id\":\"mobile\",\"userName\":\""+name+"\",\"mobile\":\""+phone+"\"}"; header.put("content-type","application/x-www-form-urlencoded"); // 3.調(diào)用接口查詢(xún) String testRst = HttpUtil.doPost("http://localhost/sv/query",newParam,header); if(testRst==null){ throw new Exception("bomc接口響應(yīng)失敗,請(qǐng)稍后重試"); } // 4.解析結(jié)果集 JSONObject json = JSONObject.parseObject(testRst); JSONArray item = json.getJSONArray("items"); JSONObject obj = item.getJSONObject(0); String account = (String) obj.get("userAccount"); return account; } public static String doPost(String url, String params, Map header) throws Exception { CloseableHttpClient httpclient = HttpClients.createDefault(); HttpPost httpPost = new HttpPost(url);// 創(chuàng)建httpPost logger.info("POST請(qǐng)求url:" + url); for (Iterator iter = header.keySet().iterator(); iter.hasNext(); ) { String key = String.valueOf(iter.next()); String value = String.valueOf(header.get(key)); httpPost.setHeader(key, value); } RequestConfig requestConfig = RequestConfig.custom().setConnectionRequestTimeout(20000) .setSocketTimeout(20000).setConnectTimeout(20000).build(); httpPost.setConfig(requestConfig); //設(shè)置參數(shù) logger.info("POST請(qǐng)求參數(shù):" + params); StringEntity entity = new StringEntity(params, "utf-8"); entity.setContentEncoding("UTF-8"); entity.setContentType("application/json"); httpPost.setEntity(entity); CloseableHttpResponse response = null; try { response = httpclient.execute(httpPost); StatusLine status = response.getStatusLine(); int state = status.getStatusCode(); if (state == HttpStatus.SC_OK) { HttpEntity responseEntity = response.getEntity(); String jsonString = EntityUtils.toString(responseEntity,"UTF-8"); return jsonString; } else { logger.error("請(qǐng)求返回:" + state + "(" + url + ")"); } } finally { if (response != null) { try { response.close(); } catch (IOException e) { e.printStackTrace(); } } try { httpclient.close(); } catch (IOException e) { e.printStackTrace(); } } return null; }
總結(jié)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
Java使用list集合remove需要注意的事項(xiàng)(使用示例)
List集合的一個(gè)特點(diǎn)是它其中的元素是有序的,也就是說(shuō)元素的下標(biāo)是根據(jù)插入的順序來(lái)的,在刪除頭部或者中間的一個(gè)元素后,后面的元素下標(biāo)會(huì)往前移動(dòng),本文給大家介紹Java使用list集合remove需要注意的事項(xiàng),感興趣的朋友一起看看吧2022-01-01java中如何實(shí)現(xiàn) zip rar 7z 壓縮包解壓
這篇文章主要介紹了java中如何實(shí)現(xiàn) zip rar 7z 壓縮包解壓?jiǎn)栴},具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-07-07Redis結(jié)合AOP與自定義注解實(shí)現(xiàn)分布式緩存流程詳解
項(xiàng)目中如果查詢(xún)數(shù)據(jù)是直接到MySQL數(shù)據(jù)庫(kù)中查詢(xún)的話,會(huì)查磁盤(pán)走IO,效率會(huì)比較低,所以現(xiàn)在一般項(xiàng)目中都會(huì)使用緩存,目的就是提高查詢(xún)數(shù)據(jù)的速度,將數(shù)據(jù)存入緩存中,也就是內(nèi)存中,這樣查詢(xún)效率大大提高2022-11-11JAVA用戶(hù)自定義事件監(jiān)聽(tīng)實(shí)例代碼
這篇文章主要介紹了JAVA用戶(hù)自定義事件監(jiān)聽(tīng)實(shí)例代碼的相關(guān)資料,需要的朋友可以參考下2017-04-04Mybatis使用collection標(biāo)簽進(jìn)行樹(shù)形結(jié)構(gòu)數(shù)據(jù)查詢(xún)時(shí)攜帶外部參數(shù)查詢(xún)
這篇文章主要介紹了Mybatis使用collection標(biāo)簽進(jìn)行樹(shù)形結(jié)構(gòu)數(shù)據(jù)查詢(xún)時(shí)攜帶外部參數(shù)查詢(xún),需要的朋友可以參考下2023-10-10java jackson 將對(duì)象轉(zhuǎn)json時(shí),忽略子對(duì)象的某個(gè)屬性操作
這篇文章主要介紹了java jackson 將對(duì)象轉(zhuǎn)json時(shí),忽略子對(duì)象的某個(gè)屬性操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-10-10