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)文章
logback?OutputStreamAppender高效日志輸出源碼解析
這篇文章主要介紹了為大家logback?OutputStreamAppender日志輸出效率提升示例解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-10-10
把spring boot項(xiàng)目發(fā)布tomcat容器(包含發(fā)布到tomcat6的方法)
這篇文章主要介紹了把spring boot項(xiàng)目發(fā)布tomcat容器(包含發(fā)布到tomcat6的方法),然后在文章給大家提到了如何將Spring Boot項(xiàng)目打包部署到外部Tomcat,需要的朋友參考下吧2017-11-11
feign name指定服務(wù)調(diào)用無效問題及解決
文章主要介紹了FeignClient注解的常用屬性,并通過一個具體的例子說明了為什么某個Feign調(diào)用需要使用url指定路徑才能訪問,最后,文章給出了解決辦法,即使用path屬性指定前綴2024-11-11
java實(shí)現(xiàn)客戶端向服務(wù)器發(fā)送文件
這篇文章主要為大家詳細(xì)介紹了java實(shí)現(xiàn)客戶端向服務(wù)器發(fā)送文件,具有一定的參考價值,感興趣的小伙伴們可以參考一下2018-01-01

