首页 > 解决方案 > C# HttpWebRequest 返回错误 403。如何从 C# 中的 HttpWebRequest 修复 403 错误

问题描述

我一直在尝试在网站上使用 a ,但在我尝试访问的特定网站上HttpWebRequest一直被禁止。403我想知道是否有任何方法可以解决它。我几乎一直在尝试添加一堆标题来解决问题,但没有任何效果:

 Uri uri = new Uri("https://www.wakfu.com");
            CookieContainer cookies = new CookieContainer();
            HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create(uri);
            ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
            myRequest.UserAgent = "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.87 Safari/537.36"; ;
            myRequest.Method = "GET";
            myRequest.CookieContainer = cookies;
            myRequest.Headers.Add("Accept-Language", "en;q=0.8");
            myRequest.Accept = "text/html";
            myRequest.Headers.Add("Accept-Encoding", "gzip, deflate");
            myRequest.ContentType = "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8";
            myRequest.Headers.Add("Upgrade-Insecure-Requests", "1");
            myRequest.Headers.Add("Accept-Language", "sk,cs;q=0.8,en-US;q=0.5,en;q=0.3");
            myRequest.Headers.Add("Upgrade-Insecure-Requests: 1");
            myRequest.KeepAlive = true;
            myRequest.Headers.Add("Cache-Control", "max-age=0");
            myRequest.Proxy.Credentials = System.Net.CredentialCache.DefaultCredentials;
            myRequest.AutomaticDecompression = (DecompressionMethods.GZip | DecompressionMethods.Deflate);
 
            //Returns 403
            HttpWebResponse myHttpWebResponse = (HttpWebResponse)myRequest.GetResponse();

标签: c#httpwebrequesthttpwebresponse

解决方案


推荐阅读