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中的更多相关文章

  1. python+pytest接口自动化(6)-请求参数格式的确定

    我们在做接口测试之前,先需要根据接口文档或抓包接口数据,搞清楚被测接口的详细内容,其中就包含请求参数的编码格式,从而使用对应的参数格式发送请求.例如某个接口规定的请求主体的编码方式为 applicat ...

  2. Action获取请求参数的3中方式

    方式一:Action本身作为Model对象,通过属性注入(Setter)方法讲参数数据封装到Action中 具体为:在Action中,提供和参数名相同的几个属性,并为其提供set方法,那么,该参数会被 ...

  3. SpringMVC @RequestBody请求参数在postman中的请求

    使用SpringMVC框架,controller使用参数  @RequestBody  LoginReq req   注解方式模拟http请求 需要请求header添加一个参数 设置  Header参 ...

  4. swagger请求参数在header中添加token

    网友大部分说的是如下配置 参照配置然而没有作用 注掉改红框内的配置,在方法上加如下注释就可以用 @ApiImplicitParams({ @ApiImplicitParam(paramType = & ...

  5. Struts2基础-4-2 -struts拦截器实现权限控制案例+ 模型驱动处理请求参数 + Action方法动态调用

    1.新建项目,添加jar包到WEB-INF目录下的lib文件夹,并添加到builde path里面 整体目录结构如下 2.新建web.xml,添加struts2核心过滤器,和默认首页 <?xml ...

  6. struts2视频学习笔记 11-12(动态方法调用,接收请求参数)

    课时11 动态方法调用 如果Action中存在多个方法时,可以使用!+方法名调用指定方法.(不推荐使用) public String execute(){ setMsg("execute&q ...

  7. jmeter将上一个接口返回值作为下一个接口的请求参数

    在jmeter中有时候会用到,将上一个接口的返回值作为下一个接口的请求参数 具体操作如下: 1.首先新建一个http请求(右键线程组--添加Sampler--http请求),同时添加好接口相应的请求参 ...

  8. Spring MVC的各种参数绑定方式(请求参数用基础类型和包装类型的区别)(转)

    1.基本数据类型(以int为例,其他类似): Controller代码: @RequestMapping("saysth.do") public void test(int cou ...

  9. HttpClient 远程接口调用方式

    远程接口调用方式HttpClient 问题:现在我们已经开发好了接口了,那该如何调用这个接口呢? 答:使用Httpclient客户端.   Httpclient简介 什么是httpclient Htt ...

随机推荐

  1. python zip 压缩

    zipfile zip文件操作 引入模块: import zipfile zip文件格式是通用的文档压缩标准,在ziplib模块中,使用ZipFile类来操作zip文件,下面具体介绍一下: zipfi ...

  2. 将本地图片数据制作成内存对象数据集|tensorflow|手写数字制作成内存对象数据集|tf队列|线程

      样本说明: tensorflow经典实例之手写数字识别.MNIST数据集. 数据集dir名称 每个文件夹代表一个标签label,每个label中有820个手写数字的图片 标签label为0的文件夹 ...

  3. python-前端Jquery

    Jquery 高级版javascript 提供了更加便利的js使用方式 楔子 需求二:将上面的li标签实现隔行换色效果 js代码 <script> var obj = document.g ...

  4. Loadrunner实现多台测试机负载时可能遇到的难题

    压测机安装的loadrunner版本为11.0 压测机器的操作系统是windows server 2016 所有的负载机器操作系统和压侧机器是一样的,都是windows server 2016 Err ...

  5. windows下基于(QPC)实现的微秒级延时

    1.为什么会写windows下微秒级延时 在上一篇 实现memcpy()函数及过程总结 中测试memcpy的效率中,测试时间的拷贝效率在微秒级别,需要使用微秒级时间间隔计数. windows下提供Qu ...

  6. Java网络编程:IP地址和端口号

    1)IP地址 用来标志网络中的一个通信实体的地址.通信实体可以是计算机,路由器等. 2)IP地址分类 IPV4:32位地址,以点分十进制表示,如192.168.0.1 IPV6:128位(16个字节) ...

  7. mysql中级操作

    解析sql执行过程 show VARIABLES like '%profil%' //查看是否开启了剖析 如没开启set profiling=1; 启用 show profiles; set @que ...

  8. oracle 数据库插入中文乱码

    一. 查询数据库编码 select userenv('language') from dual; 查询服务器编码 select * from v$nls_parameters; 推出sql查询系统编码 ...

  9. C++ 浅析移位运算

    按位左移(<<): 按二进制形式把所有的数字向左移动对应的位数,高位移出(舍弃),低位的空位补零 按位右移(>>): 按二进制形式把所有的数字向右移动对应位移位数,低位移出(舍 ...

  10. maven知识

    首先下载maven 然后win的配置环境变量 maven目录结构 --main --java --package --test --java --package resources maven 需要有 ...