Using WebClient to ping a web site

橙三吉。 提交于 2019-12-11 03:56:33

问题


I have a tiny app that I wanting to run and ping an internal web site. Here is the code:

using (var client = new WebClient())
{
    client.DownloadString("http://MyServer/dev/MyApp");
}

However, it is throwing the following error:

The remote server returned an error: (401) Unauthorized.

I have all the correct credentials to access the server. I am thinking I don't know how to use WebClient very well and I just need to set properties on the client object. Any ideas?


回答1:


I found the answer. I needed to use the NetworkCredentials() method of WebClient. See below:

    using (var client = new WebClient())
    {
        client.Credentials = new NetworkCredential ("theUser", "thePassword", "theDomain"); 
        client.DownloadString("http://MyServer/dev/MyApp");
    }

This is the URL that helped me



来源:https://stackoverflow.com/questions/12202933/using-webclient-to-ping-a-web-site

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