Why does this WebRequest to sony.com throw an exception?

落爺英雄遲暮 提交于 2019-12-13 18:16:18

问题


My goal is to write a C# method that validates if a url points to a valid online resource. It currently looks something like this:

string url = "http://www.sony.com/";
HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest;
request.Method = "HEAD";
using (HttpWebResponse response = request.GetResponse() as HttpWebResponse)
{
   var msg = response.StatusCode == HttpStatusCode.OK ? "OK" : "Dead Link";
   Console.WriteLine(msg);
}

This code throws an exception, even though I can browse to the sony url in a web browser. At first I thought their web server didn't support HEAD. Not sure if some don't? But it still throws an exception with GET. Any suggestions how to better write this code to handle cases such as this?


回答1:


This site requires User-Agent header. Add this line:

request.UserAgent = "SO/1.0";


来源:https://stackoverflow.com/questions/24542591/why-does-this-webrequest-to-sony-com-throw-an-exception

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!