httpClient的post方法使用form格式数据调用接口
Http使用post调用接口,接口只接受form表单数据格式问题?
这个问题的关键是form表单提交的是数据格式是什么样子的,使用httpClient工具类时Header信息如何写。
会影响解决问题思路的因素:
1、 一致以来都是使用json数据格式调用接口的,form表单是什么格式一时向不起来。
2、 使用form表单数据情况,多是在前台页面使用form表单提交,或使用JavaScript中的FormData对象处理提交。如果是后台httpClient工具接口如何提交
解决思路:
1、先百度看网上怎么说的,找到一个有用的帖子:https://www.cnblogs.com/zhang-can/p/7631262.html 中有一句:
ajax发送的data是个字典,是键值对的形式,在http的post请求过程中,把这种键值对转换成
k1=xxx&k2=xxx这种格式,并且会带上一个请求头:
content-type : application/x-www-form-urlencoded
2、 前台访问后台实际实现也是http协议,那使用谷歌的调试工具,模拟一个form表单提交看看请求header和Form Data的情况:

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

这是我们熟悉的格式。
根据上面的测试,修改httpClient的post工具类:
关键的地方是:post请求的Header设置
httpPost.addHeader("Content-type", "application/x-www-form-urlencoded; charset=utf-8");
在post中的参数格式为:param1=小明¶m2=12
调用时实例:
HttpRequestUtil.post(url地址
, “param1=小明¶m2=12”);
测试结果:
返回参数:{"data":{},"code":"0","msg":"处理成功"}
工具类代码:
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager; import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.nio.charset.Charset; /**
* Created by guoyanan on 2018/8/7 0007.
* 接口调用工具类
*/
public class HttpRequestUtil {
private static CloseableHttpClient httpClient; static {
PoolingHttpClientConnectionManager cm = new PoolingHttpClientConnectionManager();
cm.setMaxTotal(100);
cm.setDefaultMaxPerRoute(20);
cm.setDefaultMaxPerRoute(50);
httpClient = HttpClients.custom().setConnectionManager(cm).build();
} public static String get(String url) {
CloseableHttpResponse response = null;
BufferedReader in = null;
String result = "";
try {
HttpGet httpGet = new HttpGet(url);
RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(30000).setConnectionRequestTimeout(30000).setSocketTimeout(30000).build();
httpGet.setConfig(requestConfig);
httpGet.setConfig(requestConfig);
httpGet.addHeader("Content-type", "application/json; charset=utf-8");
httpGet.setHeader("Accept", "application/json");
response = httpClient.execute(httpGet);
in = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
StringBuffer sb = new StringBuffer("");
String line = "";
String NL = System.getProperty("line.separator");
while ((line = in.readLine()) != null) {
sb.append(line + NL);
}
in.close();
result = sb.toString();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (null != response) {
response.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
return result;
} public static String post(String url, String jsonString) {
CloseableHttpResponse response = null;
BufferedReader in = null;
String result = "";
try {
HttpPost httpPost = new HttpPost(url);
RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(30000).setConnectionRequestTimeout(30000).setSocketTimeout(30000).build();
httpPost.setConfig(requestConfig);
httpPost.setConfig(requestConfig);
httpPost.addHeader("Content-type", "application/x-www-form-urlencoded; charset=utf-8");
httpPost.setHeader("Accept", "application/json");
httpPost.setEntity(new StringEntity(jsonString, Charset.forName("UTF-8"))); response = httpClient.execute(httpPost);
in = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
StringBuffer sb = new StringBuffer("");
String line = "";
String NL = System.getProperty("line.separator");
while ((line = in.readLine()) != null) {
sb.append(line + NL);
}
in.close();
result = sb.toString();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (null != response) {
response.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
return result;
} }
httpClient的post方法使用form格式数据调用接口的更多相关文章
- 自定义HttpMessageConverter实现RestTemplate的exchange方法返回自定义格式数据
一 概述 实现如下效果代码,且可正常获取到返回数据: ResponseEntity<JsonObject> resEntity = restTemplate .exchange(url, ...
- C# HttpClient Post 参数同时上传文件 上传图片 调用接口
// 调用接口上传文件 using (var client = new HttpClient()) { using (var multipartFormDataContent = new Multip ...
- 记一次使用utl_http方法调用接口,报字符或值错误
背景:ebs系统和其他系统通过utl_http包调用接口,使用log方法记录日志. 某次调用接口,执行到记录日志行报字符或值错误. 查找原因,发现是p_str的长度超过的32767的限制. 解决办法: ...
- VUE axios 发送 Form Data 格式数据请求
axios 默认是 Payload 格式数据请求,但有时候后端接收参数要求必须是 Form Data 格式的,所以我们就得进行转换.Payload 和 Form Data 的主要设置是根据请求头的 C ...
- 2016 系统设计第一期 (档案一)jQuery ajax serialize()方法form提交数据
jQuery ajax serialize()方法form提交数据,有个很奇怪的问题,好像不能取到隐藏控件的值. //点击提交按钮保存数据 $('#btn_submitUser').click(fun ...
- 使用getJSON()方法异步加载JSON格式数据
使用getJSON()方法异步加载JSON格式数据 使用getJSON()方法可以通过Ajax异步请求的方式,获取服务器中的数组,并对获取的数据进行解析,显示在页面中,它的调用格式为: jQuery. ...
- jQuery获取JSON格式数据方法
getJSON方法: jQuery.getJSON(url,data,success(data,status,xhr)) $("button").click(function(){ ...
- Android中解析XML格式数据的方法
XML介绍:Extensible Markup Language,即可扩展标记语言 一.概述 Android中解析XML格式数据大致有三种方法: SAX DOM PULL 二.详解 2.1 SAX S ...
- JS学习笔记(3)--json格式数据的添加,删除及排序方法
这篇文章主要介绍了json格式数据的添加,删除及排序方法,结合实例形式分析了针对一维数组与二维数组的json格式数据进行增加.删除与排序的实现技巧,需要的朋友可以参考下 本文实例讲述了json格式 ...
随机推荐
- 【BZOJ3122】[Sdoi2013]随机数生成器 BSGS+exgcd+特判
[BZOJ3122][Sdoi2013]随机数生成器 Description Input 输入含有多组数据,第一行一个正整数T,表示这个测试点内的数据组数. 接下来T行,每行有五个整数p,a,b, ...
- Travel(最短路)
Travel The country frog lives in has nn towns which are conveniently numbered by 1,2,…,n1,2,…,n. Amo ...
- 男神的约会(状压dp)
有一天男神约了学姐姐去看电影,电影院有一个活动,给你一个10*10的矩阵,每一个格子上都有一个0-9的整数,表示一共十种优惠券中的一种. 观众从左上角的格子开始走,走到右下角.每走到一个有着a号优惠券 ...
- Java 自带的加密类MessageDigest类(加密MD5和SHA)
Java 自带的加密类MessageDigest类(加密MD5和SHA) - X-rapido的专栏 - CSDN博客 https://blog.csdn.net/xiaokui_wingfly/ar ...
- iOS 面试题总结
最近项目做完了 比较空闲 在网上看了一份面试题 想自己整理一下 一.为什么说Objective-C是一门动态的语言?NSUInteger和NSInteger 的区别? 静态 动态是相对的,这里的动态语 ...
- JVM 指令讲解
挺有意思的 转载记录下 转载自 https://www.cnblogs.com/f1194361820/p/8524666.html 原作者: 房继诺 JVM 指令 1.Demo 2.Clas ...
- [转载]在服务器端判断request来自Ajax请求(异步)还是传统请求(同步),x-requested-with XMLHttpRequest
在服务器端判断request来自Ajax请求(异步)还是传统请求(同步) 在服务器端判断request来自Ajax请求(异步)还是传统请求(同步): 两种请求在请求的Header不同,Ajax 异步 ...
- Oracle学习笔记—Db_name、Db_domain、Global_name、Service_name、Instance_name和Oracle_SID(转载)
转载自: Oracle中DB_NAME,SID,DB_DOMAIN,SERVICE_NAME等之间的区别 Db_name:对一个数据库(Oracle database)的唯一标识.这种表示对于单个数据 ...
- 从1到N中1的个数
示例1,2...9,10,11中有四个1 int getNumber(int n) { int count = 0; int factor = 1; int low = 0; int cur = 0; ...
- windows 和rhel,centos双系统安装
1:首先确保你先安装为windows系统,为indows7以上的把. 2:安装好为indows系统后,进入系统后把磁盘分区,分出足够的空间为安装linux. 3:再为windows下使用软碟通等工具制 ...