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


import com.xin.music.constant.Constant;
import com.xin.music.utils.ResultVOUtils;
import com.xin.music.utils.SongUtils;
import com.xin.music.vo.ResultVO;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * 音乐接口
 */
@RestController
@RequestMapping(Constant.prefixRequest + "song")
@Slf4j
public class SongController {

    /**
     * 热门歌曲
     */
    @GetMapping(value = "/popular/songs/tid/{tid}")
    public String popularSongs(@PathVariable("tid") String tid) throws IOException {
        CloseableHttpClient httpclient = HttpClients.createDefault();
        try {
            String url = "https://c.y.qq.com/qzone/fcg-bin/fcg_ucc_getcdinfo_byids_cp.fcg?disstid="+tid;
            HttpGet httpGet = new HttpGet(url);
            httpGet.setHeader("Content-Type","application/jsonp");
            httpGet.setHeader("user-agent","Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/59.0.3071.115 Safari/537.36");
            httpGet.setHeader("referer", "https://c.y.qq.com/qzone/fcg-bin/fcg_ucc_getcdinfo_byids_cp.fcg");
            CloseableHttpResponse response = httpclient.execute(httpGet);
            try {
                HttpEntity entity = response.getEntity();
                //打印目标网站输出内容
                return EntityUtils.toString(entity);
            } finally {
                response.close();
            }
        } finally {
            httpclient.close();
        }
    }

    /**
     * 解析热门歌曲列表
     */
    @GetMapping(value = "/analy/songs/tid/{tid}")
    public ResultVO analySongs(@PathVariable("tid")String tid) throws IOException {
        CloseableHttpClient httpclient = HttpClients.createDefault();
        try {
            String url = "https://y.qq.com/n/m/detail/taoge/index.html?ADTAG=myqq&from=myqq&channel=10007100&id="+tid;
            HttpGet httpGet = new HttpGet(url);
            httpGet.setHeader("Content-Type","application/jsonp");
            CloseableHttpResponse response = httpclient.execute(httpGet);
            try {
                HttpEntity entity = response.getEntity();
                String songs = SongUtils.analySongs(EntityUtils.toString(entity));
                return ResultVOUtils.success(songs);
            } finally {
                response.close();
            }
        } finally {
            httpclient.close();
        }
    }

    /**
     * 音乐搜索
     * @param key
     * @return
     * @throws IOException
     */
    @GetMapping(value = "/search/song/key/{key}")
    public ResultVO searchMusic(@PathVariable("key")String key) throws IOException {
        CloseableHttpClient httpclient = HttpClients.createDefault();
        try {
            String url = "https://c.y.qq.com/soso/fcgi-bin/search_for_qq_cp?_=1555419255194&g_tk=5381&uin=0&format=json&inCharset=utf-8&outCharset=utf-8&notice=0&platform=h5&needNewCode=1&w="+key+"&zhidaqu=1&catZhida=1&t=0&flag=1&ie=utf-8&sem=1&aggr=0&perpage=20&n=20&p=1&remoteplace=txt.mqq.all";
            HttpGet httpGet = new HttpGet(url);
            httpGet.setHeader("Content-Type","application/jsonp");
            httpGet.setHeader("user-agent","Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/59.0.3071.115 Safari/537.36");
            httpGet.setHeader("origin","http://c.y.qq.com");
            httpGet.setHeader("referer", "https://c.y.qq.com/soso/fcgi-bin/search_for_qq_cp");
            CloseableHttpResponse response = httpclient.execute(httpGet);
            try {
                HttpEntity entity = response.getEntity();
                //打印目标网站输出内容
                return ResultVOUtils.success(EntityUtils.toString(entity));
//                EntityUtils.consume(entity);

            } finally {
                response.close();
            }
        } finally {
            httpclient.close();
        }
    }

}
最近下载更多
zolscy  LV12 4月5日
SZEPEZS  LV8 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日
ewan007  LV30 2023年3月21日
最近浏览更多
fff2003  LV9 9月20日
爱在黎明破晓钱  LV7 9月14日
fei3655  LV6 8月2日
binzhuyou  LV2 7月4日
Adguard  LV3 6月25日
宋好帅  LV3 6月7日
Wwz12345  LV8 6月1日
15530067717  LV2 5月25日
JulyMagnolia  LV4 4月29日
heweimin  LV13 4月7日
顶部 客服 微信二维码 底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友