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. appium常见问题10_MAC_终端输入aapt指令报错提示"command not found"

    问题: MAC终端使用aapt指令"aapt dump badging xxx/xxx/xxx.apk"查看apk包名和activity时报错提示"command not ...

  2. HBase–RegionServer宕机恢复原理

    Region Server宕机总述 HBase一个很大的特色是扩展性极其友好,可以通过简单地加机器实现集群规模的线性扩展,而且机器的配置并不需要太好,通过大量廉价机器代替价格昂贵的高性能机器.但也正因 ...

  3. java中封装的使用方法(工具myeclipse)

    封装可以实现属性私有化,将类的属性修饰符由public改为private,如此做者,其他类就无法访问该类中被private修饰的对象,一般我们会使用setter/getter()方法实现对这些对象的访 ...

  4. python3学习笔记——数字、字符串、列表、字典、元组

    什么是python解释器? python代码在解释器中运行.解释器是代码与计算机硬件之间的软件逻辑层. python的执行过程                                      ...

  5. 想实现网页滚动一定距离底部弹出div

    <script type="text/javascript"> $(window).scroll(function () { if ($(this).scrollTop ...

  6. MAC如何与linux服务器传递文件

    scp命令可以从本地拷贝文件到Linux服务器,也可以将Linux服务器文件下载到本地 将远程/root/articaleFetch/dist目录下文件和文件夹拷贝到dist文件夹 scp root@ ...

  7. shell使用reposync同步仓库

  8. Docker配置阿里云镜像加速pull

    前言:默认Docker拉取镜像是从Docker Hub上拉取,但由于防火墙的原因,导致镜像下载非常慢.为了提高拉取镜像的速度,可以配置阿里镜像或是网易镜像加速,通过使用经验推荐配置阿里镜像. 申请个人 ...

  9. 转载 Struts2的配置 struts.xml Action详解

    在学习struts的时候,我们一定要掌握struts2的工作原理.只有当我们明确了在struts2框架的内部架构的实现过程,在配置整个struts 的框架时,可以很好的进行逻辑上的配置.接下来我就先简 ...

  10. 【leetcode】931. Minimum Falling Path Sum

    题目如下: Given a square array of integers A, we want the minimum sum of a falling path through A. A fal ...