網(wǎng)站首頁 編程語言 正文
1.application/x-www-form-urlencoded
瀏覽器的原生 表單,其中ajax也是用這種方式提交的,主要是key-value 鍵值對的形式。一般的請求方式如下圖所示:
POST HTTP/1.1 Host: test.app.com Content-Type: application/x-www-form-urlencoded Cache-Control: no-cache Postman-Token: e00dbaf5-15e8-3667-6fc5-48ee3cc89758 key1=value1&key2=value2
POST中(application/x-www-form-urlencoded)請求方式截圖,主要在key中傳入接口中定義的變量,value 中傳入值就可以進行測試接口
2.multipart/form-data
它會將表單的數(shù)據(jù)處理為一條消息,以標(biāo)簽為單元,用分隔符分開。既可以上傳鍵值對,也可以上傳文件。
由于有boundary隔離,所以multipart/form-data既可以上傳文件,也可以上傳鍵值對,它采用了鍵值對的方式,所以可以上傳多個文件,在springmvc中可以使用MultipartHttpServletRequest接收通過api根據(jù)"name"獲取不同的鍵值,也可以通過MulTipartFile數(shù)組接收多個文件。
POST HTTP/1.1 Host: test.app.com Cache-Control: no-cache Postman-Token: 59227787-c438-361d-fbe1-75feeb78047e Content-Type: multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW ------WebKitFormBoundary7MA4YWxkTrZu0gW Content-Disposition: form-data; name="filekey"; filename="" Content-Type: ------WebKitFormBoundary7MA4YWxkTrZu0gW Content-Disposition: form-data; name="textkey" tttttt ------WebKitFormBoundary7MA4YWxkTrZu0gW--
PSOT同時上傳文件和鍵值對數(shù)據(jù)
3. raw
可以上傳任意格式的文本,可以上傳text、json、xml、html等Controller接口可以通過@RequestBody 來修飾,傳入數(shù)據(jù)就是JSON格式
注意: 在使用raw 方式,如果在PostMan再測試的時候需要在headers中添加一個key-value (Content-Type: application/json 或者對應(yīng)的格式)
4.binary
相當(dāng)于Content-Type:application/octet-stream,從字面意思得知,只可以上傳二進制數(shù)據(jù),通常用來上傳文件,由于沒有鍵值,所以,一次只能上傳一個文件。
POST HTTP/1.1 Host: test.app.com Cache-Control: no-cache Postman-Token: 5ad66f08-6faa-aba0-744a-ca958b1a0fc2 undefined
?提醒:
multipart/form-data與x-www-form-urlencoded區(qū)別:
html中的form 表單有兩種:application/x-www-form-urlencoded和multipart/form-data。application/x-www-form-urlencoded是默認(rèn)的MIME內(nèi)容編碼類型,它在傳輸比較大的二進制或者文本數(shù)據(jù)時效率極低。
MIME:
簡單說,MIME類型就是設(shè)定某種擴展名的文件用一種應(yīng)用程序來打開的方式類型。服務(wù)器會將它們發(fā)送的多媒體數(shù)據(jù)的類型告訴瀏覽器,而通知手段就是說明該多媒體數(shù)據(jù)的MIME類型,服務(wù)器將 MIME標(biāo)志符放入傳送的數(shù)據(jù)中來告訴瀏覽器使用哪種插件讀取相關(guān)文件。
multipart/form-data:既可以上傳文件等二進制數(shù)據(jù),也可以上傳表單鍵值對,只是最后會轉(zhuǎn)化為一條信息。當(dāng)設(shè)置multipart/form-data,http會忽略 contentType 屬性。
x-www-form-urlencoded:只能上傳鍵值對,不能用于文件上傳。不同的field是用&區(qū)分開的。這兩個類均實現(xiàn)了HttpEntity接口,使用如下:
public static String testUpload(String url) { ? ? ? ? String result = null; ? ? ? ? CloseableHttpClient httpclient = HttpClients.createDefault(); ? ? ? ? HttpPost httppost = new HttpPost(url); ? ? ? ? try { ? ? ? ? ? ? FileBody bin = new FileBody(new File("F:\\image\\sendpix0.jpg")); ? ? ? ? ? ? StringBody comment = new StringBody("A binary file of some kind", ContentType.TEXT_PLAIN); ? ? ? ? ? ? HttpEntity reqEntity = MultipartEntityBuilder.create().addPart("bin", bin).addPart("comment", comment) ? ? ? ? ? ? ? ? ? ? .build(); ? ? ? ? ? ? httppost.setEntity(reqEntity); ? ? ? ? ? ? System.out.println("executing request " + httppost.getRequestLine()); ? ? ? ? ? ? CloseableHttpResponse response = httpclient.execute(httppost); ? ? ? ? ? ? try { ? ? ? ? ? ? ? ? int statusCode = response.getStatusLine().getStatusCode(); ? ? ? ? ? ? ? ? if (statusCode == HttpStatus.SC_OK) { ? ? ? ? ? ? ? ? ? ? result = EntityUtils.toString(response.getEntity(), "UTF-8"); ? ? ? ? ? ? ? ? } ? ? ? ? ? ? } finally { ? ? ? ? ? ? ? ? response.close(); ? ? ? ? ? ? ? ? httpclient.close(); ? ? ? ? ? ? } ? ? ? ? } catch (ClientProtocolException e) { ? ? ? ? ? ? e.printStackTrace(); ? ? ? ? } catch (IOException e) { ? ? ? ? ? ? e.printStackTrace(); ? ? ? ? } finally { ? ? ? ? ? ? try { ? ? ? ? ? ? ? ? httpclient.close(); ? ? ? ? ? ? } catch (IOException e) { ? ? ? ? ? ? ? ? e.printStackTrace(); ? ? ? ? ? ? } ? ? ? ? } ? ? ? ? return result; ? ? } ? ? public static String testParam(String url) { ? ? ? ? String result = null; ? ? ? ? CloseableHttpClient httpclient = HttpClients.createDefault(); ? ? ? ? httpclient = HttpsHelper.newHttpsCloseableClient(); ? ? ? ? HttpPost httpPost = new HttpPost(url); ? ? ? ? List<NameValuePair> params = new ArrayList<NameValuePair>(); ? ? ? ? params.add(new BasicNameValuePair("key1", "value1")); ? ? ? ? params.add(new BasicNameValuePair("key2", "value2")); ? ? ? ? try { ? ? ? ? ? ? httpPost.setEntity(new UrlEncodedFormEntity(params)); ? ? ? ? ? ? httpPost.setConfig(requestConfig); ? ? ? ? ? ? CloseableHttpResponse httpResp = httpclient.execute(httpPost); ? ? ? ? ? ? try { ? ? ? ? ? ? ? ? int statusCode = httpResp.getStatusLine().getStatusCode(); ? ? ? ? ? ? ? ? if (statusCode == HttpStatus.SC_OK) { ? ? ? ? ? ? ? ? ? ? result = EntityUtils.toString(httpResp.getEntity(), "UTF-8"); ? ? ? ? ? ? ? ? } ? ? ? ? ? ? } finally { ? ? ? ? ? ? ? ? httpResp.close(); ? ? ? ? ? ? ? ? httpclient.close(); ? ? ? ? ? ? } ? ? ? ? } catch (UnsupportedEncodingException e) { ? ? ? ? ? ? e.printStackTrace(); ? ? ? ? } catch (ClientProtocolException e) { ? ? ? ? ? ? e.printStackTrace(); ? ? ? ? } catch (IOException e) { ? ? ? ? ? ? e.printStackTrace(); ? ? ? ? } finally { ? ? ? ? ? ? try { ? ? ? ? ? ? ? ? httpclient.close(); ? ? ? ? ? ? } catch (IOException e) { ? ? ? ? ? ? ? ? e.printStackTrace(); ? ? ? ? ? ? } ? ? ? ? } ? ? ? ? return result; ? ? }
原文鏈接:https://blog.csdn.net/lq18894033018/article/details/104703978
相關(guān)推薦
- 2022-01-03 踩坑解決mongoose對已經(jīng)存在的集合查詢,查詢條件不起限制作用的問題
- 2023-01-31 golang獲取變量或?qū)ο箢愋偷膸追N方式總結(jié)_Golang
- 2022-08-26 深入理解React調(diào)度(Scheduler)原理_React
- 2023-03-15 React.memo?React.useMemo對項目性能優(yōu)化使用詳解_React
- 2022-06-12 Redis高并發(fā)場景下秒殺超賣解決方案(秒殺場景)_Redis
- 2022-04-10 關(guān)于C#中GUI編程的標(biāo)準(zhǔn)事件問題_C#教程
- 2023-01-12 C語言技巧提升之回調(diào)函數(shù)的掌握_C 語言
- 2022-09-22 Redis配置文件redis.conf詳解
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡單動態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支