java http请求 httpClient get post

24 篇文章 1 订阅
11 篇文章 0 订阅

java http请求 httpClient

第三方jar包导出(maven)

jar包下载链接(可导入)
https://mvnrepository.com/artifact/org.apache.httpcomponents/httpclient
https://mvnrepository.com/artifact/org.apache.httpcomponents/httpmime

<!-- https://mvnrepository.com/artifact/org.apache.httpcomponents/httpclient -->
<dependency>
    <groupId>org.apache.httpcomponents</groupId>
    <artifactId>httpclient</artifactId>
    <version>4.5.13</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.httpcomponents/httpmime -->
<dependency>
    <groupId>org.apache.httpcomponents</groupId>
    <artifactId>httpmime</artifactId>
    <version>4.5.13</version>
</dependency>

HttpUtils工具类

import org.apache.http.Consts;
import org.apache.http.HttpEntity;
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.methods.HttpRequestBase;
import org.apache.http.entity.mime.HttpMultipartMode;
import org.apache.http.entity.mime.MultipartEntityBuilder;
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.File;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author zhong
 * http 请求utils
 */
public class HttpUtils {
    private static HttpEntity getHttpEntity(HttpRequestBase request, Map<String, String> header) {
        CloseableHttpClient httpclient = HttpClients.createDefault();
        // 设置头部信息
        if (header != null) {
            for (Map.Entry<String, String> vo : header.entrySet()) {
                request.setHeader(vo.getKey(), vo.getValue());
            }
        }
        try {
            CloseableHttpResponse response = httpclient.execute(request);
            return response.getEntity();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public static String get(String url, Map<String, String> params, Map<String, String> header) throws IOException, CloneNotSupportedException {
        // 判断是否带参数
        if (params != null) {
            List<BasicNameValuePair> paramsList = new ArrayList<>();
            //将参数进行循环
            for (Map.Entry<String, String> vo : params.entrySet()) {
                paramsList.add(new BasicNameValuePair(vo.getKey(), vo.getValue()));
            }
            // 进行URL拼接
            url += "?" + EntityUtils.toString(new UrlEncodedFormEntity(paramsList, Consts.UTF_8));
        }

        HttpGet httpGet = new HttpGet(url);
        HttpEntity entity = getHttpEntity(httpGet, header);
        if(entity!=null){
            return EntityUtils.toString(entity);
        }
        return null;
    }


    public static String post(String url, Map<String, Object> data, Map<String, String> header) throws IOException {
        HttpPost httpPost = new HttpPost(url);
        // 判断是否带参数
        if (data!= null) {
            MultipartEntityBuilder builder = MultipartEntityBuilder.create();
            builder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
            builder.setCharset(StandardCharsets.UTF_8);
            //将参数进行循环
            for (Map.Entry<String, Object> vo : data.entrySet()) {
                if (vo.getValue() instanceof File) {
                    builder.addBinaryBody(vo.getKey(), (File) vo.getValue());
                } else if (vo.getValue() instanceof String) {
                    builder.addTextBody(vo.getKey(), (String) vo.getValue());
                }
            }
            httpPost.setEntity(builder.build());
        }
        HttpGet httpGet = new HttpGet(url);
        HttpEntity entity = getHttpEntity(httpGet, header);
        if(entity!=null){
            return EntityUtils.toString(entity);
        }
        return null;
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

卑微小钟

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值