问题
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