Spring MVC 接收POST表單請求,獲取參數總結

    前段時間遇到一個問題,在spring mvc 服務端接收post請求時,經過html 表單提交的時候,服務端可以接收到參數的值。可是使用httpclient4.3構造post請求,卻沒法接收到參數的值。html

spring 代碼:java

    @RequestMapping(value = "login.do", method = RequestMethod.POST)
    @ResponseBody
    public String login(String username, String password) throws Exception {
        return username + ":" + password;
    }

表單代碼:web

<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代碼:spring

    @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編碼上傳,mvc

@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();
        }
    }

如今服務端可以正常的接收到請求了,如今總結一下表單兩種編碼的形式app

application/x-www-form-urlencoded   空格轉換爲 "+" 加號,特殊符號轉換爲 ASCII HEX 值post

multipart/form-data    不對字符進行編碼,使用二進制數據傳輸,通常用於上傳文件,非文本的數據傳輸。ui


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傳輸的文本數據了。url

相關文章
相關標籤/搜索