Java調(diào)用參數(shù)類型是application/x-www-form-urlencoded的API問題
更新時間:2024年09月29日 08:52:58 作者:謝月
在使用Postman進行接口測試時,對于POST請求,需將請求頭設置為application/x-www-form-urlencoded,并將參數(shù)轉為String類型,通常在GET請求中,參數(shù)直接拼接在URL后,本文通過具體實例,詳細講解了參數(shù)處理的方法,適合API開發(fā)者參考
一、分析
首先用postman測試對應的接口
測試如下:

其中請求頭中content-type為application/x-www-form-urlencoded
參數(shù)是:
queryParam:{"id":"mobile","userName":"name","mobile":"12345678"}注意:
我們進行API調(diào)用時,參數(shù)需要轉為String類型,平時我們調(diào)用get請求application/x-www-form-urlencoded參數(shù)都是直接拼接在url后面, url?字段名=值&字段名=值,所以這里用同樣的方式進行參數(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接口響應失敗,請稍后重試");
}
// 4.解析結果集
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ù)
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;
}總結
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關文章
logback?OutputStreamAppender高效日志輸出源碼解析
這篇文章主要介紹了為大家logback?OutputStreamAppender日志輸出效率提升示例解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-10-10
把spring boot項目發(fā)布tomcat容器(包含發(fā)布到tomcat6的方法)
這篇文章主要介紹了把spring boot項目發(fā)布tomcat容器(包含發(fā)布到tomcat6的方法),然后在文章給大家提到了如何將Spring Boot項目打包部署到外部Tomcat,需要的朋友參考下吧2017-11-11

