我试图获取https://www.americasarmy.com/soldier/1309069
的源代码使用以下代码:
using (WebClient client = new WebClient())
{
ViewBag.htmlCode = client.DownloadString("https://www.americasarmy.com/soldier/1309069");
}
答案 0 :(得分:1)
当它返回404时,源字符串将位于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();
}
}