使用Web客户端ping一个网站 [英] Using WebClient to ping a web site

查看:154
本文介绍了使用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())
{
    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");
    }

<一个href="http://www.dreamin$c$c.net/forums/topic/242194-getting-errors-when-i-tried-to-download-files-using-credentials/"相对=nofollow>这是帮助我

这篇关于使用Web客户端ping一个网站的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆