網站首頁 編程語言 正文
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
它會將表單的數據處理為一條消息,以標簽為單元,用分隔符分開。既可以上傳鍵值對,也可以上傳文件。
由于有boundary隔離,所以multipart/form-data既可以上傳文件,也可以上傳鍵值對,它采用了鍵值對的方式,所以可以上傳多個文件,在springmvc中可以使用MultipartHttpServletRequest接收通過api根據"name"獲取不同的鍵值,也可以通過MulTipartFile數組接收多個文件。
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同時上傳文件和鍵值對數據
3. raw
可以上傳任意格式的文本,可以上傳text、json、xml、html等Controller接口可以通過@RequestBody 來修飾,傳入數據就是JSON格式
注意: 在使用raw 方式,如果在PostMan再測試的時候需要在headers中添加一個key-value (Content-Type: application/json 或者對應的格式)
4.binary
相當于Content-Type:application/octet-stream,從字面意思得知,只可以上傳二進制數據,通常用來上傳文件,由于沒有鍵值,所以,一次只能上傳一個文件。
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區別:
html中的form 表單有兩種:application/x-www-form-urlencoded和multipart/form-data。application/x-www-form-urlencoded是默認的MIME內容編碼類型,它在傳輸比較大的二進制或者文本數據時效率極低。
MIME:
簡單說,MIME類型就是設定某種擴展名的文件用一種應用程序來打開的方式類型。服務器會將它們發送的多媒體數據的類型告訴瀏覽器,而通知手段就是說明該多媒體數據的MIME類型,服務器將 MIME標志符放入傳送的數據中來告訴瀏覽器使用哪種插件讀取相關文件。
multipart/form-data:既可以上傳文件等二進制數據,也可以上傳表單鍵值對,只是最后會轉化為一條信息。當設置multipart/form-data,http會忽略 contentType 屬性。
x-www-form-urlencoded:只能上傳鍵值對,不能用于文件上傳。不同的field是用&區分開的。這兩個類均實現了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
相關推薦
- 2022-02-10 微信小程序this.triggerEvent(),父組件中使用子組件的事件
- 2023-02-07 C#實現加密exe文件_C#教程
- 2023-04-18 C生萬物C語言宏將整數二進制位的奇偶數位交換_C 語言
- 2023-10-09 $nextTick的原理及作用
- 2023-01-23 重啟后nvidia-smi命令不可執行出現“Make?sure?that?the?latest?NV
- 2022-06-12 C語言實例真題講解數據結構中單向環形鏈表_C 語言
- 2022-04-23 C#面向對象的23種設計模式介紹_C#教程
- 2022-10-22 Android協程作用域與序列發生器限制介紹梳理_Android
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支