Http Web请求,网址不存在和网址存在但下载时出现问题的区别

时间:2018-07-10 15:27:19

标签: c# httprequest webclient httpresponse

var url1 = "https://www.regulations.gov/contentStreamer?documentId=BIS-2018-0006-16235&attachmentNumber=2&contentType=pdf"; // url exist but issue when downloading
var url2 = "https://www.regulations.gov/contentStreamer?documentId=BIS-2018-0006-16010&attachmentNumber=2&contentType=pdf"; // url does not exist

try
{

    HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest;
    request.Method = "HEAD";
    HttpWebResponse response = request.GetResponse() as HttpWebResponse;
    response.Close();
}
catch (WebException ex)
{
    // i am getting http 500 server error exception when trying to see if both urls exist.
}

有人可以帮助我找到一种更好的方法来获取不存在的URL以及下载URL时出现问题的响应。

0 个答案:

没有答案