【问题标题】:I get Error 403 Forbidden when try to reach Pastebin links尝试访问 Pastebin 链接时出现错误 403 Forbidden
【发布时间】:2022-11-01 17:12:49
【问题描述】:

从昨天开始,当我尝试从 Pastebin 获取一些文本时,我收到了“错误 403 禁止”消息。 我当前的代码工作了几个月:

//...
    var url_main = "raw_pastebin_link";
    var client_main = new WebClient();
    using (var stream_main = client_main.OpenRead(url_main))
    using (var reader_main = new StreamReader(stream_main))
    {
    //Some code
    }
//...

我尝试添加一些这样的标题,但问题仍然存在。

//...
            var client_main = new WebClient();
            client_main.Headers.Add(HttpRequestHeader.UserAgent, "Mozilla/6.0 (Macintosh; Intel Mac OS X 10_15_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/84.0.4147.89 Safari/537.36.");
            using (var stream_main = client_main.OpenRead(url_main))
//...

也许 Pastebin 服务器禁止了我的 IP? (当我在浏览器中打开链接时,链接很好) 有没有办法解决这个错误? 谢谢!

【问题讨论】:

  • 确保您的 pastebin 是公开的或不公开的
  • 是的,我检查了所有链接都是公开的
  • 使用此方法添加标题client_main.Headers.Add("user-agent", "Only a Header!");
  • 试过了,我得到了同样的错误信息

标签: c# webclient


【解决方案1】:

我最近也遇到过这种情况,这是我目前在我的项目中使用的解决方案。

using System.Net.Http;

string urlData = String.Empty;
WebClient WebClient = new WebClient();

// Add headers to impersonate a web browser. Some web sites 
// will not respond correctly without these headers
WebClient.Headers.Add("User-Agent", "Mozilla/5.0 (Windows; U; Windows NT 6.1; en-GB; rv:1.9.2.12) Gecko/20101026 Firefox/3.6.12");
WebClient.Headers.Add("Accept", "*/*");
WebClient.Headers.Add("Accept-Language", "en-gb,en;q=0.5");
WebClient.Headers.Add("Accept-Charset", "ISO-8859-1,utf-8;q=0.7,*;q=0.7");

urlData = WebClient.DownloadString("enter your URL here");

让我知道这是否对您有用!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-04-19
    • 2022-12-19
    • 2013-07-18
    • 1970-01-01
    • 2017-04-25
    • 1970-01-01
    • 1970-01-01
    • 2014-07-18
    相关资源
    最近更新 更多