Java請求調(diào)用參數(shù)格式為form-data類型的接口代碼示例
接口參數(shù)使用postman調(diào)用如圖所示,只能使用form-data格式調(diào)用

使用java代碼發(fā)送http請求實現(xiàn)此種方式的接口調(diào)用
public static String doPostForm(String url, HashMap<String, String> map) throws Exception {
String result = "";
CloseableHttpClient client = null;
CloseableHttpResponse response = null;
RequestConfig defaultRequestConfig = RequestConfig.custom().setSocketTimeout(550000).setConnectTimeout(550000)
.setConnectionRequestTimeout(550000).setStaleConnectionCheckEnabled(true).build();
client = HttpClients.custom().setDefaultRequestConfig(defaultRequestConfig).build();
// client = HttpClients.createDefault();
URIBuilder uriBuilder = new URIBuilder(url);
HttpPost httpPost = new HttpPost(uriBuilder.build());
httpPost.setHeader("Connection", "Keep-Alive");
httpPost.setHeader("Charset", "UTF-8");
httpPost.setHeader("Content-Type", "application/x-www-form-urlencoded");
Iterator<Map.Entry<String, String>> it = map.entrySet().iterator();
List<NameValuePair> params = new ArrayList<NameValuePair>();
while (it.hasNext()) {
Map.Entry<String, String> entry = it.next();
NameValuePair pair = new BasicNameValuePair(entry.getKey(), entry.getValue());
params.add(pair);
}
httpPost.setEntity(new UrlEncodedFormEntity(params, "UTF-8"));
try {
response = client.execute(httpPost);
if (response != null) {
HttpEntity resEntity = response.getEntity();
if (resEntity != null) {
result = EntityUtils.toString(resEntity, "UTF-8");
}
}
} catch (ClientProtocolException e) {
throw new RuntimeException("創(chuàng)建連接失敗" + e);
} catch (IOException e) {
throw new RuntimeException("創(chuàng)建連接失敗" + e);
}
return result;
}特別說明:form的Content-Type屬性為編碼方式
- 常用有兩種:application/x-www-form-urlencoded和multipart/form-data,默認(rèn)為application/x-www-form-urlencoded。
- x-www-form-urlencoded:當(dāng)action為get時候,瀏覽器用x-www-form-urlencoded的編碼方式把form數(shù)據(jù)轉(zhuǎn)換成一個字串(name1=value1&name2=value2…),然后把這個字串a(chǎn)ppend到url后面,用?分割,加載這個新的url。
- multipart/form-data:當(dāng)action為post時候,瀏覽器把form數(shù)據(jù)封裝到http body中,然后發(fā)送到server。 如果沒有type=file的控件,用默認(rèn)的application/x-www-form-urlencoded就可以了。 但是如果有type=file的話,就要用到multipart/form-data了。瀏覽器會把整個表單以控件為單位分割,并為每個部分加上Content-Disposition(form-data或者file),Content-Type(默認(rèn)為text/plain),name(控件name)等信息,并加上分割符(boundary)。
總結(jié)
到此這篇關(guān)于Java請求調(diào)用參數(shù)格式為form-data類型的接口的文章就介紹到這了,更多相關(guān)Java請求調(diào)用參數(shù)接口內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- java?11新特性HttpClient主要組件及發(fā)送請求示例詳解
- Java通過httpclient比較重定向和請求轉(zhuǎn)發(fā)
- Java HttpClient執(zhí)行請求時配置cookie流程詳細(xì)講解
- Java HttpClient-Restful工具各種請求高度封裝提煉及總結(jié)
- java中httpclient封裝post請求和get的請求實例
- java爬蟲之使用HttpClient模擬瀏覽器發(fā)送請求方法詳解
- java發(fā)送form-data請求實現(xiàn)文件上傳的示例代碼
- Java后臺接收數(shù)據(jù)的三種方式(url、form-data與application/json)
- Java httpclient請求form-data格式并設(shè)置boundary代碼實現(xiàn)方法
相關(guān)文章
優(yōu)化spring?boot應(yīng)用后6s內(nèi)啟動內(nèi)存減半
這篇文章主要為大家介紹了優(yōu)化spring?boot后應(yīng)用6s內(nèi)啟動內(nèi)存減半的優(yōu)化示例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步早日升職加薪2022-02-02
redis發(fā)布訂閱Java代碼實現(xiàn)過程解析
這篇文章主要介紹了redis發(fā)布訂閱Java代碼實現(xiàn)過程解析,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2019-09-09

