Http请求工具【添加请求头】

文章目录

      • 一、工具代码:

一、工具代码:

package *.*;


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.ContentType;
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.util.EntityUtils;

import java.io.IOException;
import java.net.URI;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @description: Http客户端工具类
 * @title: HttpClientUtil
 * @projectName:
 * @author: xy
 * @date: 2019/11/11/10:43
 * @version: 1.0
 */

public class HttpClientUtil {
   
    /**
     * 带参数的get请求
     * @param url
     * @param param
     * @return String
     */
    public static String doGet(String url, Map<String, String> param) {
   
        // 创建Httpclient对象
        CloseableHttpClient httpclient = HttpClients.createDefault();

        String resultString = "";
        CloseableHttpResponse response = null;
        try {
   
            // 创建uri
            URIBuilder builder = new URIBuilder(url);
            if (param != null) {
   
                for (String key : param.keySet()) {
   
                    builder.addParameter(key, param.get(key));
                }
            }
            URI uri = builder.

你可能感兴趣的:(【项目总结】,Http工具)