Java調(diào)用參數(shù)類型是application/x-www-form-urlencoded的API問題
一、分析
首先用postman測試對應(yīng)的接口
測試如下:

其中請求頭中content-type為application/x-www-form-urlencoded
參數(shù)是:
queryParam:{"id":"mobile","userName":"name","mobile":"12345678"}注意:
我們進(jìn)行API調(diào)用時,參數(shù)需要轉(zhuǎn)為String類型,平時我們調(diào)用get請求application/x-www-form-urlencoded參數(shù)都是直接拼接在url后面, url?字段名=值&字段名=值,所以這里用同樣的方式進(jìn)行參數(shù)處理。
二、代碼樣例
public String getNameCode(String name,String phone) throws Exception {
// 1.組裝數(shù)據(jù),以及請求頭
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)用接口查詢
String testRst = HttpUtil.doPost("http://localhost/sv/query",newParam,header);
if(testRst==null){
throw new Exception("bomc接口響應(yī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請求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請求參數(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("請求返回:" + 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é)
以上為個人經(jīng)驗(yàn),希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
Java使用list集合remove需要注意的事項(xiàng)(使用示例)
List集合的一個特點(diǎn)是它其中的元素是有序的,也就是說元素的下標(biāo)是根據(jù)插入的順序來的,在刪除頭部或者中間的一個元素后,后面的元素下標(biāo)會往前移動,本文給大家介紹Java使用list集合remove需要注意的事項(xiàng),感興趣的朋友一起看看吧2022-01-01
java中如何實(shí)現(xiàn) zip rar 7z 壓縮包解壓
這篇文章主要介紹了java中如何實(shí)現(xiàn) zip rar 7z 壓縮包解壓問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-07-07
Redis結(jié)合AOP與自定義注解實(shí)現(xiàn)分布式緩存流程詳解
項(xiàng)目中如果查詢數(shù)據(jù)是直接到MySQL數(shù)據(jù)庫中查詢的話,會查磁盤走IO,效率會比較低,所以現(xiàn)在一般項(xiàng)目中都會使用緩存,目的就是提高查詢數(shù)據(jù)的速度,將數(shù)據(jù)存入緩存中,也就是內(nèi)存中,這樣查詢效率大大提高2022-11-11
Mybatis使用collection標(biāo)簽進(jìn)行樹形結(jié)構(gòu)數(shù)據(jù)查詢時攜帶外部參數(shù)查詢
這篇文章主要介紹了Mybatis使用collection標(biāo)簽進(jìn)行樹形結(jié)構(gòu)數(shù)據(jù)查詢時攜帶外部參數(shù)查詢,需要的朋友可以參考下2023-10-10
java jackson 將對象轉(zhuǎn)json時,忽略子對象的某個屬性操作
這篇文章主要介紹了java jackson 將對象轉(zhuǎn)json時,忽略子對象的某個屬性操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-10-10

