項目場景:
開發(fā)中遇到對接需求時候,被要求用post請求傳form-data數(shù)據(jù)的時候一臉懵逼,在postman中可以調(diào)用,但是程序中怎么調(diào)用呢。
問題描述
在postman中調(diào)用是沒問題的
?但是在程序中調(diào)用就報錯了,之前用的是HttpClient的方式請求的
public StringBuffer caller(Map<String,String> map, String strURL) {
?? ??? ?// start
?? ??? ?HttpClient httpClient = new HttpClient();
?
?? ??? ?HttpConnectionManagerParams managerParams = httpClient
?? ??? ??? ??? ?.getHttpConnectionManager().getParams();
?? ??? ?// 設(shè)置連接超時時間(單位毫秒)
?? ??? ?managerParams.setConnectionTimeout(30000);
?? ??? ?// 設(shè)置讀數(shù)據(jù)超時時間(單位毫秒)
?? ??? ?managerParams.setSoTimeout(120000);
?
?? ??? ?PostMethod postMethod = new PostMethod(strURL);
?? ??? ?// 將請求參數(shù)的值放入postMethod中
?? ??? ?String strResponse = null;
?? ??? ?StringBuffer buffer = new StringBuffer();
?? ??? ?// end
?? ??? ?try {
?? ??? ??? ?//設(shè)置參數(shù)到請求對象中
?? ??? ??? ?for(String key : map.keySet()){
?? ??? ??? ??? ?postMethod.addParameter(key, map.get(key));
?? ??? ??? ?}
?? ??? ??? ?
?? ??? ??? ?int statusCode = httpClient.executeMethod(postMethod);
?? ??? ??? ?if (statusCode != HttpStatus.SC_OK) {
?? ??? ??? ??? ?throw new IllegalStateException("Method failed: "
?? ??? ??? ??? ??? ??? ?+ postMethod.getStatusLine());
?? ??? ??? ?}
?? ??? ??? ?BufferedReader reader = null;
?? ??? ??? ?reader = new BufferedReader(new InputStreamReader(
?? ??? ??? ??? ??? ?postMethod.getResponseBodyAsStream(), "UTF-8"));
?? ??? ??? ?while ((strResponse = reader.readLine()) != null) {
?? ??? ??? ??? ?buffer.append(strResponse);
?? ??? ??? ?}
?? ??? ?} catch (Exception ex) {
?? ??? ??? ?throw new IllegalStateException(ex.toString());
?? ??? ?} finally {
?? ??? ??? ?// 釋放連接
?? ??? ??? ?postMethod.releaseConnection();
?? ??? ?}
?? ??? ?return buffer;
?? ?}
請求普通的接口沒問題,但是第三方的接口會報錯:415 Unsupported Media Type?,很明顯是請求方式的問題,然后我在請求頭加上了multipart/form-data,接口請求通了,但是報錯參數(shù)錯誤,也就是接口沒獲取到參數(shù)。
postMethod.setRequestHeader("Content-Type", "multipart/form-data");
原因分析:
form-data主要是以鍵值對的形式來上傳參數(shù),同時參數(shù)之間以&分隔符分開。我就嘗試?yán)胢ap進(jìn)行數(shù)據(jù)的的封裝Map<String,String>,結(jié)果發(fā)現(xiàn)后臺無法正確解析參數(shù),是因?yàn)閙ap封裝后并不是以&鏈接的。
解決方案:
最后利用spring來作為后端框架,form-data利用LinkedMultiValueMap對象來包裝多個參數(shù),參數(shù)以key-value形式,中間以&連接。采用restTemplate代碼的實(shí)現(xiàn)如下:文章來源:http://www.zghlxwxcb.cn/news/detail-511814.html
public String caller(Map<String,String> map, String strURL){
HttpHeaders headers = new HttpHeaders();
MultiValueMap<String, Object> map= new LinkedMultiValueMap<>();
headers.add("Content-Type", "multipart/form-data");
//設(shè)置參數(shù)到請求對象中
?? ??? ?for(String key : map.keySet()){
?? ??? ? map.add(key, map.get(key));
?? ??? ?}
HttpEntity<MultiValueMap<String, Object>> requestParams = new HttpEntity<>(map, headers);
ResponseEntity<String> response = restTemplate.postForEntity(apiUrl,requestParams,String.class);
String result =response.getBody();
return result;
}
最后沒用HttpClient 的方式,改為了restTemplate的方式。文章來源地址http://www.zghlxwxcb.cn/news/detail-511814.html
到了這里,關(guān)于spring boot實(shí)現(xiàn)postman中form-data傳參方式的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!