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

jopen 9年前發布 | 299K 次閱讀 Spring MVC Web框架

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

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




 本文由用戶 jopen 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!