接口调用post请求参数在body中
package com.ynhrm.common.utils; import com.alibaba.fastjson.JSONObject;
import lombok.Data;
import org.apache.http.Consts;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
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.DefaultHttpRequestRetryHandler;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component; import java.io.IOException;
import java.util.HashMap;
import java.util.Map; @Component
@ConfigurationProperties("http.config")
@Data
public class HttpUtils {
static String url="http://localhost:9002/system/login";
static String mobile="13294950520";
static String password="123456";
/*String url;
String mobile;
String password;*/
String result="";
public String httpPost(){
//CloseableHttpClient实现了HttpClient接口
CloseableHttpClient httpClient= HttpClients.createDefault();
HttpPost httpPost=new HttpPost(url); //创建HttpClientBuilder设置属性
HttpClientBuilder httpClientBuilder = HttpClientBuilder.create().setDefaultRequestConfig(RequestConfig.custom()
.setConnectionRequestTimeout(6000)
.setSocketTimeout(6000)
.setConnectTimeout(6000).build()).setRetryHandler(new DefaultHttpRequestRetryHandler(3, true)); //设置请求头信息
Map<String,String> map=new HashMap<>();
map.put("Accept","application/json, text/plain, */*");
map.put("Accept-Encoding","gzip, deflate");
map.put("Accept-Language","zh-CN,zh;q=0.9");
map.put("Connection","keep-alive");
map.put("Content-Type","application/json;charset=UTF-8");
map.put("User-Agent","Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.26 Safari/537.36 Core/1.63.5024.400 QQBrowser/10.0.932.400");
for (Map.Entry<String, String> entry : map.entrySet()) {
httpPost.setHeader(entry.getKey(), entry.getValue());
} //传递参数为json数据
JSONObject jsonObject=new JSONObject();
jsonObject.put("mobile",mobile);
jsonObject.put("password",password);
//创建指定内容和编码的字符串实体类
StringEntity entity=new StringEntity(jsonObject.toString(), Consts.UTF_8);
//设置请求参数
httpPost.setEntity(entity); // 创建HttpClient对象,CloseableHttpClient实例的生成器
httpClient=httpClientBuilder.build(); try {
// 发送HttpPost请求,获取返回值
CloseableHttpResponse response=httpClient.execute(httpPost);
result = EntityUtils.toString(response.getEntity(), Consts.UTF_8);
} catch (Exception e) {
e.printStackTrace();
}finally {
try {
//释放资源
httpClient.close();
} catch (Exception e) {
e.printStackTrace();
}
} return result;
} public static void main(String[] args) {
HttpUtils httpUtils = new HttpUtils();
String s = httpUtils.httpPost();
System.out.println(s);
}
}
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.2</version>
</dependency>
接口调用post请求参数在body中的更多相关文章
- python+pytest接口自动化(6)-请求参数格式的确定
我们在做接口测试之前,先需要根据接口文档或抓包接口数据,搞清楚被测接口的详细内容,其中就包含请求参数的编码格式,从而使用对应的参数格式发送请求.例如某个接口规定的请求主体的编码方式为 applicat ...
- Action获取请求参数的3中方式
方式一:Action本身作为Model对象,通过属性注入(Setter)方法讲参数数据封装到Action中 具体为:在Action中,提供和参数名相同的几个属性,并为其提供set方法,那么,该参数会被 ...
- SpringMVC @RequestBody请求参数在postman中的请求
使用SpringMVC框架,controller使用参数 @RequestBody LoginReq req 注解方式模拟http请求 需要请求header添加一个参数 设置 Header参 ...
- swagger请求参数在header中添加token
网友大部分说的是如下配置 参照配置然而没有作用 注掉改红框内的配置,在方法上加如下注释就可以用 @ApiImplicitParams({ @ApiImplicitParam(paramType = & ...
- Struts2基础-4-2 -struts拦截器实现权限控制案例+ 模型驱动处理请求参数 + Action方法动态调用
1.新建项目,添加jar包到WEB-INF目录下的lib文件夹,并添加到builde path里面 整体目录结构如下 2.新建web.xml,添加struts2核心过滤器,和默认首页 <?xml ...
- struts2视频学习笔记 11-12(动态方法调用,接收请求参数)
课时11 动态方法调用 如果Action中存在多个方法时,可以使用!+方法名调用指定方法.(不推荐使用) public String execute(){ setMsg("execute&q ...
- jmeter将上一个接口返回值作为下一个接口的请求参数
在jmeter中有时候会用到,将上一个接口的返回值作为下一个接口的请求参数 具体操作如下: 1.首先新建一个http请求(右键线程组--添加Sampler--http请求),同时添加好接口相应的请求参 ...
- Spring MVC的各种参数绑定方式(请求参数用基础类型和包装类型的区别)(转)
1.基本数据类型(以int为例,其他类似): Controller代码: @RequestMapping("saysth.do") public void test(int cou ...
- HttpClient 远程接口调用方式
远程接口调用方式HttpClient 问题:现在我们已经开发好了接口了,那该如何调用这个接口呢? 答:使用Httpclient客户端. Httpclient简介 什么是httpclient Htt ...
随机推荐
- Linux下实现MySQL数据库每天定时自动备份
使用MySQL自带的备份工具+ crontab 的方式来实现备份 1.查看磁盘挂载信息(选一个容量合适的) #df -h 2.创建备份目录 为了方便,在/home保存备份文件: cd /home/ga ...
- linux文本查看与搜索
1. cat-->全文本显示 cat file #全文本显示在终端 cat -n file #显示全文本,并显示行号 cat file1 file2 >file3 #将file1 file ...
- JS删除对象属性
项目中有些属性使用之后需要重置 var user = {}; user.name = "123"; user.age="30"; console.log(use ...
- Kindeditor在线文本编辑器过滤HTML
KindEditor.ready(function (K) { editor = K.create('textarea[name="content"]', { filterMode ...
- 购物车2.0版——python第6天
li = [{'}, {'}, {'}, {'}, {'}, ] shopping_car = {} # 定义购物车dict print('欢迎光临尚雅梦想旗舰店'.center(40)) # 先让顾 ...
- MSF——基本使用和Exploit模块(一)
MSF系列: MSF——基本使用和Exploit模块(一) MSF——Payload模块(二) MSF——Meterpreter(三) MSF——信息收集(四) MSF——Metasploit Fra ...
- 安装vue开发环境→安装淘宝镜像的时候报错
问题: npm WARN deprecated socks@1.1.10: If using 2.x branch, please upgrade to at least 2.1.6 to avoid ...
- 【Mock.js】前端模拟假数据,不用在手拼了
[Mock.js]前端模拟假数据,不用在手拼了:https://www.jianshu.com/p/8579b703a4c1
- 36.Minimum Path Sum(最小路径和)
Level: Medium 题目描述: Given a m x n grid filled with non-negative numbers, find a path from top left ...
- js五种基本数据类型:string, number, boolean, null, undefined
/** * 五种基本数据类型:string, number, boolean, null, undefined */ // undefined // 声明变量foo,未声明变量bar var foo; ...