首页>代码>spring boot+spring mvc+spring整合开发QQ音乐微信小程序(含简单服务端)>/music源码/后端源码/music/src/main/java/com/xin/music/utils/HttpClientUtil.java
001package com.xin.music.utils;
002 
003import org.apache.http.Header;
004import org.apache.http.HttpEntity;
005import org.apache.http.HttpResponse;
006import org.apache.http.NameValuePair;
007import org.apache.http.client.HttpClient;
008import org.apache.http.client.entity.UrlEncodedFormEntity;
009import org.apache.http.client.methods.HttpGet;
010import org.apache.http.client.methods.HttpPost;
011import org.apache.http.impl.client.HttpClients;
012import org.apache.http.message.BasicNameValuePair;
013import org.apache.http.util.EntityUtils;
014import org.springframework.stereotype.Component;
015 
016import java.util.ArrayList;
017import java.util.Iterator;
018import java.util.List;
019import java.util.Map;
020 
021/**
022 * HttpClient工具类
023 */
024@Component
025public class HttpClientUtil {
026 
027 
028    /**
029     * 模拟post请求,获取返回数据
030     * @param url
031     * @param map
032     * @param charest
033     * @param headerList
034     * @return
035     */
036    public static String doPost(String url, Map<String, Object> map, String charest, List<Header> headerList) {
037        HttpClient httpClient = null;
038        HttpPost httpPost = null;
039        String result = null;
040        try {
041            httpClient = HttpClients.createDefault();
042            httpPost = new HttpPost(url);
043            //设置请求头,反盗链
044            for(Header header:headerList){
045//                httpPost.setHeader(header.getName(),header.getValue());
046                httpPost.setHeader(header);
047            }
048            //设置参数
049            List<NameValuePair> list = new ArrayList<>();
050            Iterator iterator = map.entrySet().iterator();
051            while (iterator.hasNext()) {
052                Map.Entry<String, Object> elem = (Map.Entry<String, Object>) iterator.next();
053                list.add(new BasicNameValuePair(elem.getKey(), (String) elem.getValue()));
054            }
055            if (list.size() > 0) {
056                //封装实体
057                UrlEncodedFormEntity entity = new UrlEncodedFormEntity(list, charest);
058                httpPost.setEntity(entity);
059            }
060            //拿到返回结果
061            HttpResponse response = httpClient.execute(httpPost);
062            if (response != null) {
063                HttpEntity resEntity = response.getEntity();
064                if (resEntity != null) {
065                    result = EntityUtils.toString(resEntity, charest);
066                }
067            }
068        } catch (Exception e) {
069            e.printStackTrace();
070        }
071        return result;
072    }
073 
074    /**
075     * 模拟get请求
076     * @param url
077     * @param headerList
078     * @param charest
079     * @return
080     */
081    public static String doGet(String url, List<Header> headerList, String charest){
082 
083        HttpClient httpClient = null;
084        HttpGet httpGet = null;
085        String result = null;
086        try {
087            httpClient = HttpClients.createDefault();
088            httpGet = new HttpGet(url);
089            //设置请求头
090            for(Header header:headerList){
091                httpGet.setHeader(header);
092            }
093            HttpResponse httpResponse = httpClient.execute(httpGet);
094            if(httpResponse!=null){
095                HttpEntity httpEntity = httpResponse.getEntity();
096                if(httpEntity!=null){
097                    result = EntityUtils.toString(httpEntity,charest);
098                }
099            }
100        }catch (Exception e){
101            e.printStackTrace();
102        }
103        return result;
104    }
105}
最近下载更多
sgm123456  LV14 6月9日
zolscy  LV24 2024年4月5日
SZEPEZS  LV9 2024年3月8日
计算机暴龙战士  LV19 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日
最近浏览更多
1140717565  LV2 7月18日
空中飞尘  LV13 7月8日
sgm123456  LV14 5月26日
dushine  LV3 4月14日
ma406805131  LV19 2月21日
lvyga1  LV2 1月6日
krispeng  LV15 2024年12月18日
yinianhuakai4  LV8 2024年12月16日
求学的熊猫  LV11 2024年11月11日
fff2003  LV9 2024年9月20日
顶部 客服 微信二维码 底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友