【问题标题】:Exception when downloading data from HTTPS site从 HTTPS 站点下载数据时出现异常
【发布时间】:2011-06-19 02:10:54
【问题描述】:

我正在开发一个网站抓取工具/屏幕抓取工具,用于在皇家邮政网站上查找跟踪信息。不幸的是,Royal Mail 不支持 API,所以这是这样做的。

无论我做什么,我都会遇到同样的异常。 (远程服务器返回错误:(500) Internal Server Error.

我的基本代码是:

class Program
{
    static void Main(string[] args)
    {
        string url = "http://track.royalmail.com/portal/rm/track?catId=22700601&gear=authentication&forcesegment=SG-Personal";
        byte[] response;

        WebClient webClient = new WebClient();
        response = webClient.DownloadData(url);
    }
}

我使用 Fiddler 来调查浏览器进行的数据交易,以便在我的代码中模仿它。我可以看到 Royal Mail 使用 cookie,因此我尝试通过向其添加 cookie 处理程序来实现支持 cookie 的 WebClient:

public class CookieAwareWebClient : WebClient
{
    private CookieContainer m_container = new CookieContainer();

    protected override WebRequest GetWebRequest(Uri address)
    {
        WebRequest request = base.GetWebRequest(address);
        if (request is HttpWebRequest)
        {
            (request as HttpWebRequest).CookieContainer = m_container;
        }
        return request;
    }
}

但这并没有帮助你:-(

我还尝试通过 Royal Mails SSL 保护站点 (https://www.royalmail.com/portal/sme/track?catId=62200738&mediaId=63900708) 查找跟踪信息,并将凭据实现到我的 C# 程序中,但没有运气。

我现在已经碰壁了,我不断地碰到相同的教程/线程,这些教程/线程似乎对我没有任何帮助。

我希望那里有一个聪明的大脑:-)

【问题讨论】:

    标签: c# https httpwebrequest webclient


    【解决方案1】:

    如果您发送所有标头,您应该停止收到 500 错误

    string url = "http://track.royalmail.com/portal/rm/trackresults?catId=22700601&pageId=trt_rmresultspage&keyname=track_blank&_requestid=17931"; 
    using(WebClient webClient = new WebClient()) {
        webClient.Headers["User-Agent"] = "Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US; rv:1.9.2.6) Gecko/20100625 Firefox/3.6.6 (.NET CLR 3.5.30729)";
        webClient.Headers["Accept"] = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";
        webClient.Headers["Accept-Language"] = "en-us,en;q=0.5";
        webClient.Headers["Accept-Encoding"] = "    gzip,deflate";
        webClient.Headers["Accept-Charset"] = "ISO-8859-1,utf-8;q=0.7,*;q=0.7";
        byte[] response = webClient.DownloadData(url); 
    }
    

    【讨论】:

      猜你喜欢
      • 2023-03-09
      • 2012-08-24
      • 1970-01-01
      • 2017-02-01
      • 2015-03-27
      • 1970-01-01
      • 2023-03-14
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多