油管无版权音频抓包纪要

每次请求需要cookie  请求方式是get

使用HttpWebRequest 请求,cookie直接是字符串的方式加入header  支持https请求方式.返回字符串,然后对返回值过滤取下载地址等信息
 private static readonly string DefaultUserAgent = "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.108 Safari/537.36";
        /// <summary>  
        /// 创建GET方式的HTTP请求  
        /// </summary>  
        /// <param name="url">请求的URL</param>  
        /// <param name="timeout">请求的超时时间</param>  
        /// <param name="userAgent">请求的客户端浏览器信息,可以为空</param>  
        /// <param name="cookies">随同HTTP请求发送的Cookie信息,如果不需要身份验证可以为空</param>  
        /// <returns></returns>  
        public static string GetContent(string cookie, string url)
        {
            string content;
            HttpWebRequest httpRequest = (HttpWebRequest)HttpWebRequest.Create(url);
            httpRequest.Headers.Add("Cookie", cookie);
            httpRequest.Referer = url;
            httpRequest.UserAgent = DefaultUserAgent;// "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/45.0.2454.93 Safari/537.36";
            httpRequest.Accept = "*/*";
            httpRequest.ContentType = "application/x-www-form-urlencoded";
            httpRequest.Method = "GET";
           // httpRequest.Headers.Add("accept-encoding", "gzip, deflate, br");
           // httpRequest.Headers.Add("accept-language", "zh-CN,zh;q=0.9");   //此处不需要设置,设置完了会出现乱码
            httpRequest.Referer = "https://www.youtube.com/audiolibrary/music?ar=1584199017054&nv=1";

            HttpWebResponse httpResponse = (HttpWebResponse)httpRequest.GetResponse();

            using (Stream responsestream = httpResponse.GetResponseStream())
            {

              //  using (StreamReader sr = new StreamReader(responsestream, System.Text.Encoding.Default))
                using (StreamReader sr = new StreamReader(responsestream, System.Text.Encoding.UTF8))   //此处编码要设置成utf8要不中文有乱码
                {
                    content = sr.ReadToEnd();
                }
            }

            return content;
        }

 下边是乱码出现的疑问, ios

en_US.UTF-8:你说英语,你在美国,字符集是utf-8
zh_CN.UTF-8:你说中文,你在中国,字符集是utf-8

如果你的LANG环境变量是en_US.UTF-8,那么系统的菜单、程序的工具栏语言、输入法默认语言就都是英文的。

如果你的LANG环境变量是zh_CN.UTF-8,那么系统的菜单、程序的工具栏语言、输入法默认语言就都是中文的。

这两天做了一个获取cookie并且携带此cookie去请求另外一个url地址,中间携带cookie用了两种方式:
1. httpRequest.CookieContainer= cookie (此cookie为一个cookie容器对象)
2.httpRequest.Headers.Add("Cookie", cookie) (此cookie为一个cookie字符串)

测试结果:1种方式cookie失效并且丢失。2种方式携带成功并且可以成功显示已登录。

原因待查,记录下。
说明:也可能cookie容器设置的方式有问题。

附上代码:


public class Login { public string GetCookie(string postString, string postUrl) { CookieContainer cookie = new CookieContainer(); HttpWebRequest httpRequset = (HttpWebRequest)HttpWebRequest.Create(postUrl);//创建http 请求 httpRequset.CookieContainer = cookie;//设置cookie httpRequset.Method = "POST";//POST 提交 httpRequset.KeepAlive = true; httpRequset.UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/45.0.2454.93 Safari/537.36"; httpRequset.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8"; httpRequset.ContentType = "application/x-www-form-urlencoded";//以上信息在监听请求的时候都有的直接复制过来 httpRequset.Referer = "http://my.qianlima.com/login.jsp"; byte[] bytes = System.Text.Encoding.UTF8.GetBytes(postString); httpRequset.ContentLength = bytes.Length; Stream stream = httpRequset.GetRequestStream(); stream.Write(bytes, 0, bytes.Length); stream.Close();//以上是POST数据的写入 HttpWebResponse httpResponse = (HttpWebResponse)httpRequset.GetResponse();//获得 服务端响应 var str = cookie.GetCookieHeader(httpRequset.RequestUri); return str;//拿到cookie } public string GetContent(string cookie, string url) { string content; HttpWebRequest httpRequest = (HttpWebRequest)HttpWebRequest.Create(url); httpRequest.Headers.Add("Cookie", cookie); httpRequest.Referer = url; httpRequest.UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/45.0.2454.93 Safari/537.36"; httpRequest.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8"; httpRequest.ContentType = "application/x-www-form-urlencoded"; httpRequest.Method = "GET"; HttpWebResponse httpResponse = (HttpWebResponse)httpRequest.GetResponse(); using (Stream responsestream = httpResponse.GetResponseStream()) { using (StreamReader sr = new StreamReader(responsestream, System.Text.Encoding.Default)) { content = sr.ReadToEnd(); } } return content; } }
最后就是获取到json数据 在线编辑一下.生成实体类,很方便
https://www.sojson.com/

 先校验/格式化

一行代码转换存入类实体

  Root rt = JsonConvert.DeserializeObject<Root>(content);


复制代码
原文地址:https://www.cnblogs.com/zuochanzi/p/12501474.html