使用Web客户端ping一个网站客户端、网站、Web、ping

由网友(背叛后的深拥受不起)分享简介:我有一个很小的应用程序,我想运行及平安内部网站。这里是code: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())...

我有一个很小的应用程序,我想运行及平安内部网站。这里是code:

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:

远程服务器返回错误:(401)未经授权

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

我把所有访问服务器正确的凭据。我想我不知道如何使用Web客户端非常好,我只需要在客户对象上设置的属性。任何想法?

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?

推荐答案

我找到了答案。我需要使用NetworkCredentials()Web客户端的方法。请看下图:

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

这是帮助我

阅读全文

相关推荐

最新文章