首页>代码>spring boot+spring mvc+spring整合开发QQ音乐微信小程序(含简单服务端)>/music源码/后端源码/music/src/main/java/com/xin/music/utils/HttpClientUtil.java
001 | package com.xin.music.utils; |
002 |
003 | import org.apache.http.Header; |
004 | import org.apache.http.HttpEntity; |
005 | import org.apache.http.HttpResponse; |
006 | import org.apache.http.NameValuePair; |
007 | import org.apache.http.client.HttpClient; |
008 | import org.apache.http.client.entity.UrlEncodedFormEntity; |
009 | import org.apache.http.client.methods.HttpGet; |
010 | import org.apache.http.client.methods.HttpPost; |
011 | import org.apache.http.impl.client.HttpClients; |
012 | import org.apache.http.message.BasicNameValuePair; |
013 | import org.apache.http.util.EntityUtils; |
014 | import org.springframework.stereotype.Component; |
015 |
016 | import java.util.ArrayList; |
017 | import java.util.Iterator; |
018 | import java.util.List; |
019 | import java.util.Map; |
020 |
021 | /** |
022 | * HttpClient工具类 |
023 | */ |
024 | @Component |
025 | public 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 | } |


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日