HttpClient

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

public class HttpUtil{
/**
* 携带请求参数的GET请求
*
* @param url
* @param param
* @return
*/
public static String doGet(String url, Map<String, String> param,String cookie) {
//
String result = “”;
// 1.创建Httpclient对象
CloseableHttpClient httpclient = HttpClients.createDefault();
CloseableHttpResponse response = null;
try {
// 2.创建uri对象
URIBuilder builder = new URIBuilder(url);
if (param != null) {
for (String key : param.keySet()) {
builder.addParameter(key, param.get(key));
}
}
URI uri = builder.build();
// 3.创建http GET请求
HttpGet httpGet = new HttpGet(uri);

        //添加头部信息
        httpGet.addHeader("Cookie",cookie);

        // 4.执行请求
        response = httpclient.execute(httpGet);
        // 5.判断返回状态是否为200
        if (response.getStatusLine().getStatusCode() == 200) {
            // 6.进行UTF-8编码处理
            result = EntityUtils.toString(response.getEntity(), "UTF-8");
        }
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        try {
            if (response != null) {
                response.close();
            }
            httpclient.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    return result;
}

public static String doGetWithHeaders(String url, Map<String, String> params) {
    //
    String result = "";
    // 1.创建Httpclient对象
    CloseableHttpClient httpclient = HttpClients.createDefault();
    CloseableHttpResponse response = null;
    try {
        // 2.创建uri对象
        URIBuilder builder = new URIBuilder(url);
        URI uri = builder.build();
        // 3.创建http GET请求
        HttpGet httpGet = new HttpGet(uri);

        //添加头部信息
        if(params != null){
            Set<Map.Entry<String, String>> entries = params.entrySet();
            for (Map.Entry<String, String> entry : entries) {
                httpGet.addHeader(entry.getKey(),entry.getValue());
            }
        }
        // 4.执行请求
        response = httpclient.execute(httpGet);
        // 5.判断返回状态是否为200
        if (response.getStatusLine().getStatusCode() == 200) {
            // 6.进行UTF-8编码处理
            result = EntityUtils.toString(response.getEntity(), "UTF-8");
        }
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        try {
            if (response != null) {
                response.close();
            }
            httpclient.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    return result;
}
/**
 * 不需要携带参数的get请求
 *
 * @param url
 * @return
 */
public static String doGet(String url,String cookie) {
    return doGet(url, null,cookie);
}
/**
 * 携带请求参数的POST请求
 *
 * @param url
 * @param param
 * @return
 */
public static String doPost(String url, Map<String, String> param) {
    //
    String result = "";
    // 1.创建Httpclient对象
    CloseableHttpClient httpClient = HttpClients.createDefault();
    CloseableHttpResponse response = null;
    try {
        // 2.创建Http Post请求
        HttpPost httpPost = new HttpPost(url);
        // 3.创建参数列表
        if (param != null) {
            List<BasicNameValuePair> paramList = new ArrayList<>();
            for (String key : param.keySet()) {
                paramList.add(new BasicNameValuePair(key, param.get(key)));
            }
            // 4.模拟表单
            UrlEncodedFormEntity entity = new UrlEncodedFormEntity(paramList,"UTF-8");
            httpPost.setEntity(entity);
        }
        // 5.执行http请求
        response = httpClient.execute(httpPost);
        // 6.获取响应的结果
        result = EntityUtils.toString(response.getEntity(), "UTF-8");
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        try {
            response.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    return result;
}
/**
 * 发送无携带请求参数的POST请求
 *
 * @param url
 * @return
 */
public static String doPost(String url) {
    return doPost(url, null);
}
/**
 * 以json的方式传递请求参数,发送POST请求
 *
 * @param url
 * @param json
 * @return
 */
public static String doPostJson(String url, String json) {//map json
    //
    String result = "";
    // 1.创建Httpclient对象
    CloseableHttpClient httpClient = HttpClients.createDefault();
    CloseableHttpResponse response = null;
    try {
        // 2.创建Http Post请求
        HttpPost httpPost = new HttpPost(url);
        // 3.创建请求内容
        StringEntity entity = new StringEntity(json, ContentType.APPLICATION_JSON);
        httpPost.setEntity(entity);
        // 4.执行http请求
        response = httpClient.execute(httpPost);
        // 5.获取响应结果
        result = EntityUtils.toString(response.getEntity(), "utf-8");
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        try {
            response.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    return result;
}

}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值