本文介绍了附加信息:远程服务器返回错误:(404)找不到. MVC的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试获取https://www.americasarmy.com/soldier/1309069
使用以下代码:
using (WebClient client = new WebClient())
{
ViewBag.htmlCode = client.DownloadString("https://www.americasarmy.com/soldier/1309069");
}
推荐答案
当返回404时,源字符串将位于WebException.Response中:
As it returns 404, the source string will be in the WebException.Response :
try
{
client.DownloadString("https://www.americasarmy.com/soldier/1309069");
}
catch (WebException webex)
{
using (var streamReader = new StreamReader(webex.Response.GetResponseStream()))
{
var htmlCode = streamReader.ReadToEnd();
}
}
这篇关于附加信息:远程服务器返回错误:(404)找不到. MVC的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!