Interface calls post request parameters in the body

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));

        // setup request header 
        the Map <String, String> Map = new new the 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());
        }


        // pass data parameters json 
        the JSONObject jsonObject = new new the JSONObject ();
        jsonObject.put("mobile",mobile);
        jsonObject.put ( "password" , password);
         // create a string to specify the content and coding entity class 
        StringEntity Entity = new new StringEntity (jsonObject.toString (), Consts.UTF_8);
         // set the request parameter 
        httpPost.setEntity (entity );

        // Create HttpClient object instance generator CloseableHttpClient 
        httpClient = httpClientBuilder.build ();

        the try {
             // send HttpPost request to obtain the return value 
            CloseableHttpResponse Response = httpClient.execute (HttpPost);
            result = EntityUtils.toString(response.getEntity(), Consts.UTF_8);
        } catch (Exception e) {
            e.printStackTrace ();
        } The finally {
             the try {
                 // release resources 
                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>

Guess you like

Origin www.cnblogs.com/yscec/p/11946642.html