spring mvc如何接收httpclient post参数呢?

戚薇 SpringMVC 发布时间:2022-07-18 09:27:19 阅读数:17267 1
下文笔者讲述spring mvc接收post参数的方法分享,如下所示
实现思路:
    spring mvc中接收参数
	只需在方法的参数中定义同表单中name一致
	则可接收参数信息
例:
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", "java265");
            multipartEntityBuilder.addTextBody("password", "888888");
            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
 
@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", "java265"));
            params.add(new BasicNameValuePair("password", "888888"));
            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();
        }
    }

注意事项
   采用以上方式,spring mvc可正常接收到请求
   下文将列表接收请求的编码方式  
    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传输的文本数据
版权声明

本文仅代表作者观点,不代表本站立场。
本文系作者授权发表,未经许可,不得转载。

本文链接: https://www.Java265.com/JavaFramework/SpringMVC/202207/3998.html

最近发表

热门文章

好文推荐

Java265.com

https://www.java265.com

站长统计|粤ICP备14097017号-3

Powered By Java265.com信息维护小组

使用手机扫描二维码

关注我们看更多资讯

java爱好者