java调用接口返回form表单_httpClient的post方法使用form格式数据调用接口

  • Post author:
  • Post category:java

Http使用post调用接口,接口只接受form表单数据格式问题?

这个问题的关键是form表单提交的是数据格式是什么样子的,使用httpClient工具类时Header信息如何写。

会影响解决问题思路的因素:

1、         一致以来都是使用json数据格式调用接口的,form表单是什么格式一时向不起来。

2、         使用form表单数据情况,多是在前台页面使用form表单提交,或使用JavaScript中的FormData对象处理提交。如果是后台httpClient工具接口如何提交

解决思路:

ajax发送的data是个字典,是键值对的形式,在http的post请求过程中,把这种键值对转换成

k1=xxx&k2=xxx这种格式,并且会带上一个请求头:

content-type : application/x-www-form-urlencoded

2、 前台访问后台实际实现也是http协议,那使用谷歌的调试工具,模拟一个form表单提交看看请求header和Form Data的情况:

edf51549739682322c1fdc9cfabfdd06.png

点击“view source”显示的格式:

17ba16fb7a4a469cad9a1e743e323681.png

这是我们熟悉的格式。

根据上面的测试,修改httpClient的post工具类:

关键的地方是:post请求的Header设置

httpPost.addHeader(“Content-type”, “application/x-www-form-urlencoded; charset=utf-8”);

在post中的参数格式为:param1=小明&param2=12

调用时实例:

HttpRequestUtil.post(url地址

, “param1=小明&param2=12”);

测试结果:

返回参数:{“data”:{},”code”:”0″,”msg”:”处理成功”}

工具类代码:

1 importorg.apache.http.client.config.RequestConfig;2 importorg.apache.http.client.methods.CloseableHttpResponse;3 importorg.apache.http.client.methods.HttpGet;4 importorg.apache.http.client.methods.HttpPost;5 importorg.apache.http.entity.StringEntity;6 importorg.apache.http.impl.client.CloseableHttpClient;7 importorg.apache.http.impl.client.HttpClients;8 importorg.apache.http.impl.conn.PoolingHttpClientConnectionManager;9

10 importjava.io.BufferedReader;11 importjava.io.IOException;12 importjava.io.InputStreamReader;13 importjava.nio.charset.Charset;14

15 /**

16 * Created by guoyanan on 2018/8/7 0007.17 * 接口调用工具类18 */

19 public classHttpRequestUtil {20 private staticCloseableHttpClient httpClient;21

22 static{23 PoolingHttpClientConnectionManager cm = newPoolingHttpClientConnectionManager();24 cm.setMaxTotal(100);25 cm.setDefaultMaxPerRoute(20);26 cm.setDefaultMaxPerRoute(50);27 httpClient =HttpClients.custom().setConnectionManager(cm).build();28 }29

30 public staticString get(String url) {31 CloseableHttpResponse response = null;32 BufferedReader in = null;33 String result = “”;34 try{35 HttpGet httpGet = newHttpGet(url);36 RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(30000).setConnectionRequestTimeout(30000).setSocketTimeout(30000).build();37 httpGet.setConfig(requestConfig);38 httpGet.setConfig(requestConfig);39 httpGet.addHeader(“Content-type”, “application/json; charset=utf-8”);40 httpGet.setHeader(“Accept”, “application/json”);41 response =httpClient.execute(httpGet);42 in = new BufferedReader(newInputStreamReader(response.getEntity().getContent()));43 StringBuffer sb = new StringBuffer(“”);44 String line = “”;45 String NL = System.getProperty(“line.separator”);46 while ((line = in.readLine()) != null) {47 sb.append(line +NL);48 }49 in.close();50 result =sb.toString();51 } catch(IOException e) {52 e.printStackTrace();53 } finally{54 try{55 if (null !=response) {56 response.close();57 }58 } catch(IOException e) {59 e.printStackTrace();60 }61 }62 returnresult;63 }64

65 public staticString post(String url, String jsonString) {66 CloseableHttpResponse response = null;67 BufferedReader in = null;68 String result = “”;69 try{70 HttpPost httpPost = newHttpPost(url);71 RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(30000).setConnectionRequestTimeout(30000).setSocketTimeout(30000).build();72 httpPost.setConfig(requestConfig);73 httpPost.setConfig(requestConfig);74 httpPost.addHeader(“Content-type”, “application/x-www-form-urlencoded; charset=utf-8”);75 httpPost.setHeader(“Accept”, “application/json”);76 httpPost.setEntity(new StringEntity(jsonString, Charset.forName(“UTF-8”)));77

78 response =httpClient.execute(httpPost);79 in = new BufferedReader(newInputStreamReader(response.getEntity().getContent()));80 StringBuffer sb = new StringBuffer(“”);81 String line = “”;82 String NL = System.getProperty(“line.separator”);83 while ((line = in.readLine()) != null) {84 sb.append(line +NL);85 }86 in.close();87 result =sb.toString();88 } catch(IOException e) {89 e.printStackTrace();90 } finally{91 try{92 if (null !=response) {93 response.close();94 }95 } catch(IOException e) {96 e.printStackTrace();97 }98 }99 returnresult;100 }101

102 }


版权声明:本文为weixin_34462016原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。