从 HTTPS 站点下载数据时出现异常

发布于 2024-10-15 08:46:53 字数 1384 浏览 2 评论 0原文

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

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

我的基本代码是:

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 来调查浏览器进行的数据事务,以便在我的代码中模仿它。我可以看到皇家邮政使用 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;
    }
}

但这并没有帮助 :-(

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

我现在遇到了障碍,而且我不断遇到相同的教程/线程 。似乎对我没有任何帮助,

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

I am working on a siteripper / screenscraper for looking up tracking information on the Royal Mail website. Unfortunately Royal Mail do not support an API, so this is the way to do it.

I keep getting the same exception no matter what I do.
(The remote server returned an error: (500) Internal Server Error.)

My base code is:

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);
    }
}

I have used Fiddler, to investigate the data transactions made by my browser in order to mimic that in my code. I can see Royal Mail uses cookies, so I have tried to implement a WebClient that supports cookies by adding a cookie handler to it:

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;
    }
}

But that didn't help eather :-(

I have also tried to look up the tracking information through Royal Mails SSL protected site (https://www.royalmail.com/portal/sme/track?catId=62200738&mediaId=63900708), and implementing credentials into my C# program, but no luck there.

I have now meet the wall, and I keep bumping into the same tutorials / threads that don't seem to help me any further.

I hope there is a brilliant brain out there :-)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

眼泪也成诗 2024-10-22 08:46:53

如果您发送所有标头,您应该停止收到 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); 
}

If you send all the headers you should stop getting the 500 error

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); 
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文