HTTPUTILS

maven依赖

<dependency>
			<groupId>org.apache.httpcomponents</groupId>
			<artifactId>httpclient</artifactId>
			<version>4.5.6</version>
		</dependency>
		<!-- https://mvnrepository.com/artifact/com.alibaba/fastjson -->
		<dependency>
			<groupId>com.alibaba</groupId>
			<artifactId>fastjson</artifactId>
			<version>1.2.47</version>
		</dependency>

  

工具类

package com.yyjdemo.shardingjdbc.http;

import com.alibaba.fastjson.JSONObject;
import org.apache.http.HttpEntity;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
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.client.utils.URIBuilder;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.params.HttpParams;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class HttpUtils {
    public static void main(String[] args) throws IOException {

        JSONObject jsonObject = new JSONObject();
        jsonObject.put("id","1");
//        String httppost = httppost("http://127.0.0.1:8080/jdbc/testpost",jsonObject);
//        System.out.println(httppost);
//        String httppost = httppostjson("http://127.0.0.1:8080/jdbc/testpostjson",jsonObject);
        String httppost = httpget("http://127.0.0.1:8080/jdbc/test",jsonObject);
    }

    public static String httpget(String url, JSONObject jsonObject) {
        try {
            URIBuilder uriBuilder = new URIBuilder(url);
            if(!jsonObject.isEmpty()){
                jsonObject.forEach((k,v)->{
                    uriBuilder.addParameter(k,v.toString());
                });
            }

            HttpGet httpget = new HttpGet(uriBuilder.toString());
            try(CloseableHttpClient httpclient = HttpClients.createDefault();
                CloseableHttpResponse execute = httpclient.execute(httpget);){
                HttpEntity entity = execute.getEntity();
                String s = EntityUtils.toString(entity);
                return s;
            }
        }catch (Exception e){
            e.printStackTrace();
        }
     return null;
    }

    public static String httppostjson(String url, JSONObject jsonObject) {
        try {
            HttpPost httpPost = new HttpPost(url);
            httpPost.addHeader(HTTP.CONTENT_TYPE,"application/json");
            StringEntity stringEntity = new StringEntity(jsonObject.toString(),"utf-8");
            httpPost.setEntity(stringEntity);
            try(CloseableHttpClient httpclient = HttpClients.createDefault();
                CloseableHttpResponse execute = httpclient.execute(httpPost);){
                HttpEntity entity = execute.getEntity();
                String s = EntityUtils.toString(entity);
                return s;
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }

    private static String httppost(String url,JSONObject jsonObject){
        try {
            HttpPost httpPost = new HttpPost(url);
            List<NameValuePair> objects = new ArrayList<>();
            jsonObject.forEach((k,v)->{
                BasicNameValuePair basicNameValuePair = new BasicNameValuePair(k, v.toString());
                objects.add(basicNameValuePair);
            });
            StringEntity stringEntity = new UrlEncodedFormEntity(objects,"utf-8");
            httpPost.setEntity(stringEntity);
            try(CloseableHttpClient httpclient = HttpClients.createDefault();
                CloseableHttpResponse execute = httpclient.execute(httpPost);){
                HttpEntity entity = execute.getEntity();
                String s = EntityUtils.toString(entity);
                return s;
        }
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }
}

  

原文地址:https://www.cnblogs.com/yeyongjian/p/10201888.html