从 HTTPS 站点下载数据时出现异常
我正在开发一个网站抓取器/屏幕抓取器,用于在皇家邮政网站上查找跟踪信息。不幸的是,皇家邮政不支持 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您发送所有标头,您应该停止收到 500 错误
If you send all the headers you should stop getting the 500 error