postmanUtils工具类,模拟postman的get,post请求

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请求结束------------------------------------------");

    }

}

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值