当前位置:网站首页>The postmanutils tool class simulates the get and post requests of postman
The postmanutils tool class simulates the get and post requests of postman
2022-06-22 05:16: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(" Being implemented post request ------------------------------------------");
System.out.println(" Read posturl------------------"+posturl);
System.out.println(" Read postparam------------------"+postparam);
//1、 open postman
// This step is equivalent to running main Method .
//2、 establish request Connect 3、 Fill in url And how to request
HttpPost httpPost = new HttpPost(posturl);
//2.1 Extra settings Content-Type Request header
httpPost.setHeader("Content-Type","application/x-www-form-urlencoded;charset=UTF-8");
//4、 If there are parameters, add parameters
CloseableHttpClient client = HttpClients.createDefault();
httpPost.setEntity(new StringEntity(postparam,"UTF-8"));
//5、 Click Send button , Send a request 6、 Get response message
CloseableHttpResponse response = client.execute(httpPost);
//7、 Formatted response message
HttpEntity entity = response.getEntity();
String result = EntityUtils.toString(entity);
//8、 Output message at the console
System.out.println(" The result of the request is ----------------------"+result);
System.out.println(" perform post End of request ------------------------------------------");
}
@PostConstruct
public void getutil() throws Exception{
System.out.println("get------------------------------------------");
System.out.println(" Read posturl------------------"+geturl);
//1、 open postman
// This step is equivalent to running main Method .
//2、 establish request Connect 3、 Fill in url And how to request
HttpGet get = new HttpGet(geturl);
//4、 If there are parameters, add parameters get The request does not require parameters , Omit
CloseableHttpClient client = HttpClients.createDefault();
//5、 Click Send button , Send a request 6、 Get response message
CloseableHttpResponse response = client.execute(get);
//7、 Formatted response message
HttpEntity entity = response.getEntity();
String result = EntityUtils.toString(entity);
//8、 Output message at the console
System.out.println(" The result of the request is ----------------------"+result);
System.out.println(" perform get End of request ------------------------------------------");
}
}
边栏推荐
- Accelerate the promotion of industrial Internet, and map out a new blueprint for development
- YARN 应用提交过程
- Reconstructing thinking series 2-functions and variables
- 9. Gateway cross domain processing
- flink部署模式总结
- ActiveMQ knowledge summary of Message Oriented Middleware
- mysql day04课堂笔记
- Squoosh - 谷歌出品的免费开源图片压缩工具,图片大小减少90%!支持 API 开发调用
- Qt保存QTextEdit内存至.txt文件中
- Hash type of redis
猜你喜欢

Squoosh - 谷歌出品的免费开源图片压缩工具,图片大小减少90%!支持 API 开发调用

Progress warning and problem management of progress control in Zhiyuan project management SPM system

数据库---基础知识

在Vs Code中搭建JSP开发环境

毕业回馈!Apache Doris 社区所有贡献者来领礼品啦!

Pytest (12) -allure common features allure attach、allure. step、fixture、environment、categories
![[chrome] Google tips Google browser comes with scrolling screenshot (full screen shot)](/img/9b/6ad704959a77abf9cb28fdd33b81b5.png)
[chrome] Google tips Google browser comes with scrolling screenshot (full screen shot)

Remote Dictionary Server(Redis)——基于 KV 结构的作为 Cache 使用的 NoSQL 数据库管理系统

Storage mode and lifetime of C language variables

flink部署模式总结
随机推荐
Monorepo絲滑方法論:引用模塊熱更新
1108. Defanging an IP Address
TIDB-升级版本
Start with the strategy of small market value factor, and take you into quantitative investment (with the strategy of 77.83% annualized return)
这是一个图片
mysql day01课堂笔记
Chapter 6 infinite series_ Fourier series
使用选择法对十个整数进行排序
mysql day02课堂笔记
Lottie components make animation easier
Restframework read and non read sequence processing
Sort ten integers using selection
YARN 应用提交过程
Analysis of T elevator repair in 2022 and simulation test questions of T elevator repair
restframework读取和非读取序列处理
SQL分类,用户属性
yarn部署模式依赖预上传设置
C语言自定义函数的一些注意事项
Tupu software 2D and 2.5D case collection | smart Park, data center, SMT production line
Flink deployment mode (II) - three deployment modes of yarn