C#使用 WebRequest 異步獲取網頁並自動忽略SSL證書


C#使用 WebRequest 模擬瀏覽器請求訪問網頁並自動忽略HTTPS安全證書

以下兩個C#異步方法,封裝了WebRequest請求,支持忽略SSL證書。

 

作者:張賜榮

 

1.Get請求
        public static Task<string> HTTP_Get(string URL, string[] headers = null)  // HTTP Get 請求
        {
            Task<string> ts = Task.Run(() => {
                System.Net.HttpWebRequest hwr = System.Net.HttpWebRequest.Create(URL) as System.Net.HttpWebRequest;
                hwr.Proxy = null;
                hwr.Method = "GET";
                hwr.UserAgent = @"Mozilla/5.0 (Windows NT 10.0; Win64; x64; Trident/7.0; MSIE/11.0; rv:11.0;) like Gecko";
                hwr.Referer = URL;
                hwr.ServerCertificateValidationCallback = (_s, _x509s, _x509c, _ssl) => { return (true); };
                try
                {
                    if (headers != null)
                    {
                        foreach (var item in headers)
                        {
                            hwr.Headers.Add(item);
                        }
                    }
                    using (System.IO.StreamReader sr = new System.IO.StreamReader(hwr.GetResponseAsync().Result.GetResponseStream()))
                    {
                        return (sr.ReadToEnd());
                    }
                }
                catch (Exception ex)
                {
                    Program.Log(ex.ToString());
                    return ("");
                }
            });
            return (ts);
        }
2.Post 請求:
        public static Task<string> HTTP_Post(string URL, string Data, string[] headers = null)  // HTTP Post 請求
        {
            Task<string> ts = Task.Run(() => {
                System.Net.HttpWebRequest hwr = System.Net.HttpWebRequest.Create(URL) as System.Net.HttpWebRequest;
                hwr.Method = "POST";
                hwr.Proxy = null;
                hwr.ContentType = "application/x-www-form-urlencoded";
                hwr.UserAgent = @"Mozilla/5.0 (Windows NT 10.0; Win64; x64; Trident/7.0; MSIE/11.0; rv:11.0;) like Gecko";
                hwr.ServerCertificateValidationCallback = (_s, _x509s, _x509c, _ssl) => { return (true); };
                try
                {
                    if (headers != null)
                    {
                        foreach (var item in headers)
                        {
                            hwr.Headers.Add(item);
                        }
                    }
                    byte[] tmp = Encoding.GetEncoding("UTF-8").GetBytes(Data);
                    hwr.GetRequestStream().WriteAsync(tmp, 0, tmp.Length);
                    using (System.IO.StreamReader sr = new System.IO.StreamReader(hwr.GetResponseAsync().Result.GetResponseStream()))
                    {
                        return (sr.ReadToEnd());
                    }
                }
                catch (Exception ex)
                {
                    Program.Log(ex.ToString());
                    return ("");
                }
            });
            return (ts);
        }

 

參考:
WebRequest 文檔:
https://docs.microsoft.com/zh-cn/dotnet/api/system.net.webrequest?redirectedfrom=MSDN&view=net-6.0

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM