当前位置:网站首页>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 ------------------------------------------");

    }

}

原网站

版权声明
本文为[a good idea]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/173/202206220510552690.html