Additional information: The remote server returned an error: (404) Not Found. MVC

蓝咒 提交于 2019-12-23 01:18:38

问题


I'm trying to get the source code of https://www.americasarmy.com/soldier/1309069

using the following code:

using (WebClient client = new WebClient())
{

    ViewBag.htmlCode = client.DownloadString("https://www.americasarmy.com/soldier/1309069");
}

回答1:


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(); 
    }
}


来源:https://stackoverflow.com/questions/42398465/additional-information-the-remote-server-returned-an-error-404-not-found-mv

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!