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,默認為application/x-www-form-urlencoded。
- x-www-form-urlencoded:當action為get時候,瀏覽器用x-www-form-urlencoded的編碼方式把form數(shù)據(jù)轉(zhuǎn)換成一個字串(name1=value1&name2=value2…),然后把這個字串a(chǎn)ppend到url后面,用?分割,加載這個新的url。
- multipart/form-data:當action為post時候,瀏覽器把form數(shù)據(jù)封裝到http body中,然后發(fā)送到server。 如果沒有type=file的控件,用默認的application/x-www-form-urlencoded就可以了。 但是如果有type=file的話,就要用到multipart/form-data了。瀏覽器會把整個表單以控件為單位分割,并為每個部分加上Content-Disposition(form-data或者file),Content-Type(默認為text/plain),name(控件name)等信息,并加上分割符(boundary)。
總結
到此這篇關于Java請求調(diào)用參數(shù)格式為form-data類型的接口的文章就介紹到這了,更多相關Java請求調(diào)用參數(shù)接口內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
- java?11新特性HttpClient主要組件及發(fā)送請求示例詳解
- Java通過httpclient比較重定向和請求轉(zhuǎn)發(fā)
- Java HttpClient執(zhí)行請求時配置cookie流程詳細講解
- Java HttpClient-Restful工具各種請求高度封裝提煉及總結
- 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格式并設置boundary代碼實現(xiàn)方法
相關文章
Java ThreadLocal原理解析以及應用場景分析案例詳解
這篇文章主要介紹了Java ThreadLocal原理解析以及應用場景分析案例詳解,本篇文章通過簡要的案例,講解了該項技術的了解與使用,以下就是詳細內(nèi)容,需要的朋友可以參考下2021-09-09最新IntelliJ?IDEA?2022配置?Tomcat?8.5?的詳細步驟演示
這篇文章主要介紹了IntelliJ?IDEA?2022?詳細配置?Tomcat?8.5?步驟演示,本文通過圖文并茂的形式給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-08-08Spring整合TimerTask實現(xiàn)定時任務調(diào)度
這篇文章主要介紹了Spring整合TimerTask實現(xiàn)定時任務調(diào)度的相關資料,具有一定的參考價值,感興趣的小伙伴們可以參考一下2016-12-12使用SSM+Layui+Bootstrap實現(xiàn)汽車維保系統(tǒng)的示例代碼
本文主要實現(xiàn)對汽車維修廠的信息化管理功能,。實現(xiàn)的主要功能包含用戶管理、配置管理、汽車管理、故障管理、供應商管理、配件管理、維修訂單管理、統(tǒng)計信息、公告管理、個人信息管理,感興趣的可以了解一下2021-12-12