当前位置:网站首页>postmanUtils工具类,模拟postman的get,post请求
postmanUtils工具类,模拟postman的get,post请求
2022-06-22 05:11:00 【a good idea】
package com.demo.common;
import org.apache.http.HttpEntity;
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.util.EntityUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import javax.annotation.PostConstruct;
@Configuration
public class PostMethod {
@Value("${post.url}")
private String posturl;
@Value("${post.param}")
private String postparam;
@Value("${get.url}")
private String geturl;
@PostConstruct
public void postutil() throws Exception{
System.out.println("正在执行post请求------------------------------------------");
System.out.println("读取posturl------------------"+posturl);
System.out.println("读取postparam------------------"+postparam);
//1、打开postman
//这一步相当于运行main方法。
//2、创建request连接 3、填写url和请求方式
HttpPost httpPost = new HttpPost(posturl);
//2.1 额外设置Content-Type请求头
httpPost.setHeader("Content-Type","application/x-www-form-urlencoded;charset=UTF-8");
//4、如果有参数添加参数
CloseableHttpClient client = HttpClients.createDefault();
httpPost.setEntity(new StringEntity(postparam,"UTF-8"));
//5、点击发送按钮,发送请求 6、获取响应报文
CloseableHttpResponse response = client.execute(httpPost);
//7、格式化响应报文
HttpEntity entity = response.getEntity();
String result = EntityUtils.toString(entity);
//8、在控制台输出报文
System.out.println("请求结果是----------------------"+result);
System.out.println("执行post请求结束------------------------------------------");
}
@PostConstruct
public void getutil() throws Exception{
System.out.println("get------------------------------------------");
System.out.println("读取posturl------------------"+geturl);
//1、打开postman
//这一步相当于运行main方法。
//2、创建request连接 3、填写url和请求方式
HttpGet get = new HttpGet(geturl);
//4、如果有参数添加参数 get请求不需要参数,省略
CloseableHttpClient client = HttpClients.createDefault();
//5、点击发送按钮,发送请求 6、获取响应报文
CloseableHttpResponse response = client.execute(get);
//7、格式化响应报文
HttpEntity entity = response.getEntity();
String result = EntityUtils.toString(entity);
//8、在控制台输出报文
System.out.println("请求结果是----------------------"+result);
System.out.println("执行get请求结束------------------------------------------");
}
}
边栏推荐
- Analysis of T elevator repair in 2022 and simulation test questions of T elevator repair
- mysql day03课堂笔记
- Create a new local content and upload it to the code cloud branch
- C语言自定义函数的一些注意事项
- Concurrent programming - thread pool
- NFC authentication makes the ultimate KYC experience
- long start = currentTimeMillis();
- JedisSentinel 工具类
- zipimport.ZipImportError:
- DTS migration script sqlserver
猜你喜欢

Tupu software 2D and 2.5D case collection | smart Park, data center, SMT production line
![Reading notes on how MySQL works [updating]](/img/4a/56cff3db0d45e66f51beb490c3ae71.jpg)
Reading notes on how MySQL works [updating]

VirtualBox 6.1.34 release

Flynk deployment mode summary

Target detection algorithm based on deep learning interview essential (rcnn~yolov5)
Solve the shortage of developers. Maybe it can help you

Solutions to MySQL 8.0 public key retrieval is not allowed errors

6. Local - custom filter factory

Kubernetes——部署应用到集群中

Remote dictionary server (redis) - a kV based NoSQL database management system used as a cache
随机推荐
Some notes on the use of C language strings
MySQL day01 class notes
yarn部署模式依赖预上传设置
Daemon flow
Go learning (II. Built in container)
Which methods are not allowed to be overridden?
It is easy to analyze and improve R & D efficiency by understanding these five figures
新建本地内容上传至码云分支
获取DPI函数返回值永远是96 | 获取DPI函数返回值不正确 | GetDpiForMonitor/GetDeviceCaps返回值不正确的原因
Software architecture and pattern: structure, component and relationship
Compilation des connaissances communes de la base de données numpy
守护进程的流程
Progress warning and problem management of progress control in Zhiyuan project management SPM system
Flink deployment mode (II) - three deployment modes of yarn
并发编程——线程池
Great! Huaibei and Huaibei enterprises are approved to use special marks for geographical indication products
6. Local - custom filter factory
9. Gateway cross domain processing
mysql day02课堂笔记
Reconstructing thinking series 2-functions and variables