java 使用httpclient进行post/get请求


import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
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.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;


/**
 * @Auther: 
 * @Date: 2020/10/10 18:43
 * @Description:调用http请求的post、get
 */
public class httpRequest {

    /*
     * get请求
     * */
    public static void getRequest(String url, Map<String, String> headers, Map<String, Object> params) {
        StringBuffer uris = new StringBuffer();
        CloseableHttpClient httpClient = HttpClients.createDefault();

        //遍历传参
        if (params.size() > 0) {
            Iterator<Map.Entry<String, Object>> itparams = params.entrySet().iterator();
            while (itparams.hasNext()) {
                Map.Entry<String, Object> entry = itparams.next();
                String property = entry.getKey();
                String value = entry.getValue().toString();
                uris.append(property + "=" + value + "&");//拼接url 请求参数
            }
        }
        uris.delete(uris.lastIndexOf("&"), uris.length());//删除最后一个&字符
        //调get请求
        HttpGet httpGet = new HttpGet(url + "?" + uris);
        //遍历请求头,设置
        Iterator<Map.Entry<String, String>> it = headers.entrySet().iterator();
        while (it.hasNext()) {
            Map.Entry<String, String> entry = it.next();
            String property = entry.getKey();
            String value = entry.getValue();
            httpGet.addHeader(property, value); //添加请求头
        }

        try {
            HttpResponse response = httpClient.execute(httpGet);
            HttpEntity httpEntity = response.getEntity();//获得响应的实体对象
            String result = EntityUtils.toString(httpEntity, "utf-8");
            System.out.println(result);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /*
     * post请求
     * */
    public static void postRequest(String url, Map<String, String> headers, List<NameValuePair> params) {
        CloseableHttpClient httpClient = HttpClients.createDefault();
        HttpPost httpPost = new HttpPost(url);
        //遍历请求头,设置
        Iterator<Map.Entry<String, String>> it = headers.entrySet().iterator();
        while (it.hasNext()) {
            Map.Entry<String, String> entry = it.next();
            String property = entry.getKey();
            String value = entry.getValue();
            httpPost.setHeader(property, value); //添加请求头
        }
        //遍历传参
        if (params.size() > 0) {

            try {
                httpPost.setEntity(new UrlEncodedFormEntity(params, "utf-8"));
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }

        }
        //调post请求
        CloseableHttpResponse response = null;
        try {
            response = httpClient.execute(httpPost);

            HttpEntity httpEntity = response.getEntity();//获得响应的实体对象
            String result = EntityUtils.toString(httpEntity, "utf-8");
            System.out.println(result);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值