前段時間遇到一個問題,在spring mvc 服務端接收post請求時,通過html 表單提交的時候,服務端能夠接收到參數的值。但是使用httpclient4.3構造post請求,卻無法接收到參數的值。
同樣的,通過PostMan請求POST接口提交數據時,只能接收x-www-form-urlencoded數據,卻無法接收 form-data數據.
spring 代碼:
@RequestMapping(value = "login.do", method = RequestMethod.POST)
@ResponseBody
public String login(String username, String password) throws Exception {
return username + ":" + password;
}
表單代碼:
<form action="http://localhost:8080/test/login.do" id="frm" method="post"> name:<input type="text" name="username" id="username"/> </br> psword:<input type="text" name="password" id="password"/> </br> <input id="submit" type="submit" /> </form>
httpclient4.3發送post代碼:
@Test
public void testMultipartPost() throws IOException {
HttpPost httpPost = new HttpPost("http://localhost:8080/test/login.do");
try {
HttpClientBuilder httpClientBuilder = HttpClientBuilder.create();
CloseableHttpClient httpClient = httpClientBuilder.build();
RequestConfig config = RequestConfig.custom().setConnectTimeout(200000).setSocketTimeout(200000).build();
httpPost.setConfig(config);
MultipartEntityBuilder multipartEntityBuilder = MultipartEntityBuilder.create();
multipartEntityBuilder.setCharset(Charset.forName("UTF-8"));
multipartEntityBuilder.addTextBody("username", "taozi");
multipartEntityBuilder.addTextBody("password", "123");
HttpEntity httpEntity = multipartEntityBuilder.build();
httpPost.setEntity(httpEntity);
HttpResponse response = httpClient.execute(httpPost);
System.out.println(EntityUtils.toString(response.getEntity()));
} finally {
httpPost.releaseConnection();
}
}
為什么通過httpclient4.3構造的post請求,服務端無法接收到傳輸的參數。比較與html的差異,發現httpclient構造的請求使用的是multipart形式。而表單上傳使用的是默認形式的編碼,x-www-form-urlencoded,所以表單能夠成功。
現在找到問題了,將httpclient的構造代碼,改為x-www-form-urlencoded編碼上傳,
@Test
public void testUrlencodedPost() throws IOException {
HttpPost httpPost = new HttpPost("http://localhost:8080/test/login.do");
try {
CloseableHttpClient client = HttpClients.createDefault();
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("username", "taozi"));
params.add(new BasicNameValuePair("password", "123"));
HttpEntity httpEntity = new UrlEncodedFormEntity(params, "UTF-8");
httpPost.setEntity(httpEntity);
CloseableHttpResponse response = client.execute(httpPost);
System.out.println(EntityUtils.toString(response.getEntity()));
} finally {
httpPost.releaseConnection();
}
}
現在服務端能夠正常的接收到請求了,現在總結一下表單兩種編碼的形式.
application/x-www-form-urlencoded 空格轉換為 "+" 加號,特殊符號轉換為 ASCII HEX 值
multipart/form-data 不對字符進行編碼,使用二進制數據傳輸,一般用於上傳文件,非文本的數據傳輸。
spring mvc如果要接收 multipart/form-data 傳輸的數據,應該在spring上下文配置
<bean id="multipartResolver"
class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
</bean>
這樣服務端就既可以接收multipart/form-data 傳輸的數據,也可以接收application/x-www-form-urlencoded傳輸的文本數據了。
PostMan中 form-data、x-www-form-urlencoded、raw、binary的區別:
1、form-data:
就是http請求中的multipart/form-data,它會將表單的數據處理為一條消息,以標簽為單元,用分隔符分開。既可以上傳鍵值對,也可以上傳文件。當上傳的字段是文件時,會有Content-Type來表名文件類型;content-disposition,用來說明字段的一些信息;
由於有boundary隔離,所以multipart/form-data既可以上傳文件,也可以上傳鍵值對,它采用了鍵值對的方式,所以可以上傳多個文件。


2、x-www-form-urlencoded:
就是application/x-www-from-urlencoded,會將表單內的數據轉換為鍵值對,比如,name=java&age = 23


3、raw
可以上傳任意格式的文本,可以上傳text、json、xml、html等


4、binary
相當於Content-Type:application/octet-stream,從字面意思得知,只可以上傳二進制數據,通常用來上傳文件,由於沒有鍵值,所以,一次只能上傳一個文件。
multipart/form-data與x-www-form-urlencoded區別
multipart/form-data:既可以上傳文件等二進制數據,也可以上傳表單鍵值對,只是最后會轉化為一條信息;
x-www-form-urlencoded:只能上傳鍵值對,並且鍵值對都是間隔分開的。
