失败这里是我的代码的HttpClient在访问简单的网站
internal static void ValidateUrl(string url)
{
Uri validUri;
if(Uri.TryCreate(url,UriKind.Absolute,out validUri))
{
using (HttpClient client = new HttpClient())
{
try
{
HttpResponseMessage response = client.Get(url);
response.EnsureStatusIsSuccessful();
}
catch (Exception ex)
{
//exception handler goes here
}
}
}
}
该代码当我运行它会产生这样的结果。
ProxyAuthenticationRequired (407) is not one of the following:
OK (200), Created (201), Accepted (202), NonAuthoritativeInformation
(203), NoContent (204), ResetContent (205), PartialContent (206).
我想要做的就是让这段代码验证给定的网站是否正常运行。 任何想法?
嗯,我不会调用一个方法抛出一个异常只是为了吞下该异常...如果有属性来检查不会抛出 –
是真的,但我并不想偏离太多的OPs版本。无论如何改变。 –
所有答案都是有效的,因为它使用最新的4.5异步await关键字。我会坚持这一个。谢谢@Oliver – Sike12