『梓喵·技术』使用C#调用网易云音乐API的简单测试与分析

本文参考的部分资料已在结尾列出。
截至2017.08.03,大部分接口已失效,新版接口已加密。
新版请参考:网易云音乐新版WebAPI分析

目录:

1、网易云音乐相关API
2、使用c#调用
3、简单的测试成品

网易云音乐的相关API

这里的网易云API为Moon大神以及其他大神等得出,本人也抓包进行了验证。Moon大神的文章链接已在文中及结尾列出。

1、歌曲信息:

Get方法:http://music.163.com/api/song/detail/?id=歌曲id&ids=[歌曲id]&csrf_token=
当id正确时返回格式:

{"songs":[{
	"starred":false,
	"popularity":5.0,
	"starredNum":0,
	"playedNum":0,
	"dayPlays":0,
	"hearTime":0,
	"mp3Url":"http://m2.music.126.net/oJe-1zZhVL4PcElir7W96A==/5649290743614436.mp3",
	"rtUrls":[],
	"artists":[{"img1v1Id":18686200114669622,"topicPerson":0,"picUrl":"http://p3.music.126.net/6y-UleORITEDbvrOLV0Q8A==/5639395138885805.jpg","picId":0,"albumSize":0,"img1v1Url":"http://p4.music.126.net/VnZiScyynLG7atLIZ2YPkw==/18686200114669622.jpg","briefDesc":"","trans":"","alias":[],"musicSize":0,"name":"坂本昌一郎","id":14593,"img1v1Id_str":"18686200114669622"}],
	"hMusic":{"bitrate":192000,"playTime":178938,"dfsId":5649290743614437,"volumeDelta":0.289741,"sr":44100,"name":"Black One","id":35417247,"size":3825822,"extension":"mp3"},"mMusic":{"bitrate":160000,"playTime":178938,"dfsId":5649290743614435,"volumeDelta":0.688485,"sr":44100,"name":"Black One","id":35417245,"size":3627638,"extension":"mp3"},"lMusic":{"bitrate":96000,"playTime":178938,"dfsId":5649290743614436,"volumeDelta":0.864432,"sr":44100,"name":"Black One","id":35417246,"size":2195710,"extension":"mp3"},"album":{"songs":[],"paid":false,"onSale":false,"picUrl":"http://p3.music.126.net/GmgDv08JNU1gn9wVUBzIVw==/931286348726508.jpg","artists":[{"img1v1Id":18686200114669622,"topicPerson":0,"picUrl":"http://p3.music.126.net/6y-UleORITEDbvrOLV0Q8A==/5639395138885805.jpg","picId":0,"albumSize":0,"img1v1Url":"http://p3.music.126.net/VnZiScyynLG7atLIZ2YPkw==/18686200114669622.jpg","briefDesc":"","trans":"","alias":[],"musicSize":0,"name":"坂本昌一郎","id":14593,"img1v1Id_str":"18686200114669622"}],"picId":931286348726508,"artist":{"img1v1Id":18686200114669622,"topicPerson":0,"picUrl":"http://p4.music.126.net/6y-UleORITEDbvrOLV0Q8A==/5639395138885805.jpg","picId":0,"albumSize":0,"img1v1Url":"http://p4.music.126.net/VnZiScyynLG7atLIZ2YPkw==/18686200114669622.jpg","briefDesc":"","trans":"","alias":[],"musicSize":0,"name":"","id":0,"img1v1Id_str":"18686200114669622"},"publishTime":1261670400000,"commentThreadId":"R_AL_3_44451","briefDesc":"","copyrightId":0,"company":"5pb. Records","status":1,"tags":"","description":"","subType":null,"alias":["罪与罚与赎的少女 电视动画原声带"],"blurPicUrl":"http://p3.music.126.net/GmgDv08JNU1gn9wVUBzIVw==/931286348726508.jpg","companyId":0,"pic":931286348726508,"name":"TV Animation 11eyes (O.S.T)","id":44451,"type":"专辑","size":55},"mvid":0,"fee":0,"ftype":0,"rtype":0,"rurl":null,"commentThreadId":"R_SO_4_458964","copyrightId":0,"score":5,"copyFrom":"","status":0,"disc":"","no":45,"audition":null,"ringtone":"","position":45,"duration":178938,"alias":[],"crbt":null,"bMusic":{"bitrate":96000,"playTime":178938,"dfsId":5649290743614436,"volumeDelta":0.864432,"sr":44100,"name":"Black One","id":35417246,"size":2195710,"extension":"mp3"},"rtUrl":null,"name":"Black One","id":458964
        }],
 "equalizers":{},
 "code":200
}

 

2、 歌曲搜索(需要特定的头信息):

Post方法:http://music.163.com/api/search/pc
必要参数:s:搜索的内容
offset:偏移量(分页用)
limit:获取的数量
type:搜索的类型(歌曲 1 专辑 10歌手 100歌单 1000用户 1002mv 1004歌词 1006主播电台 1009)
返回格式(需使用正确的头信息才能访问,否则403)篇幅过长,略。

3、 歌曲下载:

Get方法:http://music.163.com/api/song/enhance/download/url?br=码率&id=歌曲id
当id正确时的返回格式:

{"data":{
	"id":28445467,
	"url":"http://m10.music.126.net/20161019192149/64bd58d3071e829f606afee85e63d501/ymusic/fe64/c17f/54cc/64c15c1430e55bf2c8093f0b47dc39f4.mp3",
	"br":320000,
	"size":6732382,
	"md5":"64c15c1430e55bf2c8093f0b47dc39f4",
	"code":200,
	"expi":1200,
	"type":"mp3",
	"gain":-1.34,
	"fee":0,
	"uf":null,
	"payed":0,
	"flag":0,
	"canExtend":false},
"code":200}
4、 头信息

必须的参数:cookie appver键值与http Referer

5、专辑、歌单等等

详见moonlib大神的此篇文章:网易云音乐常用API浅析

使用c#调用

c#白痴一枚,抛砖引玉。

1、获取歌曲信息
public static string getMusic(string id) {
            string url = "http://music.163.com/api/song/detail/?id="+id+"&ids=["+id+"]&csrf_token=";

            HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create(url);
            req.Method = "get";
            HttpWebResponse response = (HttpWebResponse)req.GetResponse();
            Stream res = response.GetResponseStream();
            StreamReader reader1 = new StreamReader(res);
            string a1 = reader1.ReadToEnd();
	    return a1;
            //string[] a2 = a1.Split(new string[]{"\"mp3Url\":\"","\",\"rtUrls"},StringSplitOptions.RemoveEmptyEntries);
            //this.mp3url = a2[1];
	    }

 

2、获取下载地址
public static string getApiUrl(int kbps, string id)
        {
            return "http://music.163.com/api/song/enhance/download/url?br=" + kbps + "&id=" + id;
        }

public static string getDownUrl(int kbps,string id)
        {
            if (id != "")
            {
                
                string apiurl = getApiUrl(kbps, id);
                HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create(apiurl);
                req.Method = "get";
                HttpWebResponse response = (HttpWebResponse)req.GetResponse();
                Stream res = response.GetResponseStream();
                StreamReader reader1 = new StreamReader(res);
                string a1 = reader1.ReadToEnd();
		return a1;
             }
             else
             {
              return("null");
             }

        }
3、 搜索
public string Param(string sname)
        {
            string postls;
            string ss = HttpUtility.UrlEncode(sname,Encoding.GetEncoding("utf-8"));
            StringBuilder bulider = new StringBuilder();
            bulider.AppendFormat("s={0}&offset={1}&limit={2}&type={3}", ss, 0, 20, 1);
            postls = bulider.ToString();
            return postls;
        }
	public static string searchMusic(string sname) {

            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(searchurl);
            request.Method = "POST";
            request.Referer = "http://music.163.com/search/";
            request.ContentType = "application/x-www-form-urlencoded";
            string postinfo = Param(sname);
            request.ContentLength = Encoding.UTF8.GetByteCount(postinfo);
            CookieContainer cookies = new CookieContainer();
            Cookie cook = new Cookie();
            Cookie cook1 = new Cookie();
            cook.Value = "1.5.2";
            cook.Name = "appver";
            cook.Domain = "music.163.com";
            cook1.Value = "http://music.163.com/";
            cook1.Name = "Referer";
            cook1.Domain = "music.163.com";
            cookies.Add(cook);
            cookies.Add(cook1);
            request.CookieContainer = cookies;
            Stream myRequestStream = request.GetRequestStream();
            byte[] postm = Encoding.ASCII.GetBytes(postinfo);
            using(Stream reqStream =request.GetRequestStream()){
                reqStream.Write(postm,0,postm.Length);
            }

            HttpWebResponse resp = (HttpWebResponse)request.GetResponse();
            
            Stream strem = resp.GetResponseStream();
            StreamReader a1 = new StreamReader(strem);
            string a2 = a1.ReadToEnd();
            return a2;
            
        }

 

简单的测试成品

使用歌曲下载的api做了个小程序,可实现输入id获取下载链接(通过对字符串的截取和合法性判断等获取mp3直链)并下载。

引用资料

1.[内容] 【moonlib.com】 Moon 网易云音乐常用API浅析

梓喵出没博客(azimiao.com)版权所有,转载请注明链接:https://www.azimiao.com/1328.html
欢迎加入梓喵出没博客交流群:313732000

我来吐槽

*

*