BiliRanking.Core.BiliInterface.GetHtmlAsync C# (CSharp) Метод

GetHtmlAsync() публичный статический Метод

public static GetHtmlAsync ( string url ) : Task
url string
Результат Task
        public static async Task<string> GetHtmlAsync(string url)
        {
            Log.Debug("获取网页 - " + url);
            try
            {
                //using (WebClient myWebClient = new WebClient())
                //{
                WebClient myWebClient = new WebClient();
                    myWebClient.Headers.Add("Cookie", cookie);
                    myWebClient.Headers.Add("User-Agent", "Mozilla / 5.0(Windows NT 5.1) AppleWebKit / 537.36(KHTML, like Gecko) Chrome / 35.0.3319.102 Safari / 537.36");
                    //myWebClient.Headers.Add("User-Agent", "Mozilla / 5.0 BiliDroid/3.3.0 ([email protected])");
                    Random ran = new Random();
                    int ip4 = ran.Next(1, 255);
                    int select = ran.Next(1, 2);
                    string ip;
                    if (select == 1)
                        ip = "220.181.111." + ip4;
                    else
                        ip = "59.152.193." + ip4;
                    myWebClient.Headers.Add("Client-IP", ip);

                //myWebClient.Headers.Add(HttpRequestHeader.KeepAlive, "TRUE");

                    byte[] myDataBuffer = await myWebClient.DownloadDataTaskAsync(new Uri(url));

                    string sContentEncoding = myWebClient.ResponseHeaders["Content-Encoding"];
                    if (sContentEncoding == "gzip")
                    {
                        MemoryStream ms = new MemoryStream(myDataBuffer);
                        MemoryStream msTemp = new MemoryStream();
                        int count = 0;
                        GZipStream gzip = new GZipStream(ms, CompressionMode.Decompress);
                        byte[] buf = new byte[1000];
                        while ((count = gzip.Read(buf, 0, buf.Length)) > 0)
                        {
                            msTemp.Write(buf, 0, count);
                        }
                        myDataBuffer = msTemp.ToArray();
                    }
                    return Encoding.UTF8.GetString(myDataBuffer);
                //}
                
            }
            catch (Exception e)
            {
                Log.Error("获取失败!请检查网路设置!" + e.Message);
                return null;
                //throw new Exception("获取失败");
            }
        }
    }

Usage Example

Пример #1
0
        public async Task <UserInfoModel> GetMyUserInfo()
        {
            if (IsLogin())
            {
                try
                {
                    string url = string.Format("http://account.bilibili.com/api/myinfo?access_key={0}&appkey={1}&platform=wp&type=json", BiliApiHelper.access_key, BiliApiHelper._appKey_IOS);
                    url += "&sign=" + BiliApiHelper.GetSign(url);
                    string results = await BiliInterface.GetHtmlAsync(url);

                    UserInfoModel model = JsonConvert.DeserializeObject <UserInfoModel>(results);
                    //AttentionList = model.attentions;
                    return(model);
                }
                catch (Exception)
                {
                    return(null);
                }
            }
            else
            {
                return(null);
            }
        }