【问题标题】:Using WebClient to ping a web site使用 WebClient ping 网站
【发布时间】:2012-08-31 03:42:41
【问题描述】:

我有一个小应用程序,我想运行并 ping 一个内部网站。代码如下:

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

但是,它抛出以下错误:

远程服务器返回错误:(401) Unauthorized.

我拥有访问服务器的所有正确凭据。我在想我不知道如何很好地使用 WebClient,我只需要在客户端对象上设置属性。有什么想法吗?

【问题讨论】:

  • 网站使用什么认证方式?另外,您是否只想 ping 服务器已启动?如果是这样,您只需发出 HEAD 请求并确保它不是 500 代码或不响应连接。
  • 那么您是要 ping (ICMP) 还是“ping”(只需验证服务器和应用程序是否已启动)
  • 你可以争辩说你已经实现了你的目标......
  • 该网站正在使用 Windows 身份验证....我想“ping”服务器以使其保持活动状态。初始页面加载的性能很慢
  • 如果您在 Internet Explorer 中输入该 URL,它会出现吗?或者你得到错误?如果你使用 Chrome 或 Firefox 呢?

标签: c# .net webclient http-authentication


【解决方案1】:

我找到了答案。我需要使用 WebClient 的 NetworkCredentials() 方法。见下文:

    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

【讨论】:

    猜你喜欢
    • 2010-11-12
    • 1970-01-01
    • 2011-06-12
    • 2023-03-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-08-13
    相关资源
    最近更新 更多