首页>代码>spring boot+spring mvc+spring整合开发QQ音乐微信小程序(含简单服务端)>/music源码/后端源码/music/src/main/java/com/xin/music/utils/HttpClientUtil.java
package com.xin.music.utils;

import org.apache.http.Header;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

/**
 * HttpClient工具类
 */
@Component
public class HttpClientUtil {


    /**
     * 模拟post请求,获取返回数据
     * @param url
     * @param map
     * @param charest
     * @param headerList
     * @return
     */
    public static String doPost(String url, Map<String, Object> map, String charest, List<Header> headerList) {
        HttpClient httpClient = null;
        HttpPost httpPost = null;
        String result = null;
        try {
            httpClient = HttpClients.createDefault();
            httpPost = new HttpPost(url);
            //设置请求头,反盗链
            for(Header header:headerList){
//                httpPost.setHeader(header.getName(),header.getValue());
                httpPost.setHeader(header);
            }
            //设置参数
            List<NameValuePair> list = new ArrayList<>();
            Iterator iterator = map.entrySet().iterator();
            while (iterator.hasNext()) {
                Map.Entry<String, Object> elem = (Map.Entry<String, Object>) iterator.next();
                list.add(new BasicNameValuePair(elem.getKey(), (String) elem.getValue()));
            }
            if (list.size() > 0) {
                //封装实体
                UrlEncodedFormEntity entity = new UrlEncodedFormEntity(list, charest);
                httpPost.setEntity(entity);
            }
            //拿到返回结果
            HttpResponse response = httpClient.execute(httpPost);
            if (response != null) {
                HttpEntity resEntity = response.getEntity();
                if (resEntity != null) {
                    result = EntityUtils.toString(resEntity, charest);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

    /**
     * 模拟get请求
     * @param url
     * @param headerList
     * @param charest
     * @return
     */
    public static String doGet(String url, List<Header> headerList, String charest){

        HttpClient httpClient = null;
        HttpGet httpGet = null;
        String result = null;
        try {
            httpClient = HttpClients.createDefault();
            httpGet = new HttpGet(url);
            //设置请求头
            for(Header header:headerList){
                httpGet.setHeader(header);
            }
            HttpResponse httpResponse = httpClient.execute(httpGet);
            if(httpResponse!=null){
                HttpEntity httpEntity = httpResponse.getEntity();
                if(httpEntity!=null){
                    result = EntityUtils.toString(httpEntity,charest);
                }
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return result;
    }
}
最近下载更多
zolscy  LV12 4月5日
SZEPEZS  LV8 3月8日
计算机暴龙战士  LV16 2023年12月17日
天才不常有  LV9 2023年11月30日
susuha  LV6 2023年9月4日
z1556262  LV5 2023年7月24日
卡布琪洛  LV4 2023年5月29日
廖业贵  LV18 2023年5月15日
mutouhh  LV5 2023年4月13日
ewan007  LV29 2023年3月21日
最近浏览更多
heweimin  LV12 4月7日
zolscy  LV12 4月5日
li739784276  LV5 3月26日
silences_ftjh  LV10 3月22日
3263394665  LV9 3月15日
SZEPEZS  LV8 3月8日
hjy100200  LV6 1月2日
zz030701  LV1 2023年12月27日
万魔殿 2023年12月19日
暂无贡献等级
计算机暴龙战士  LV16 2023年12月17日
顶部 客服 微信二维码 底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友