2012-08-30 41 views
2

我有一个小的应用程序,我想运行并ping一个内部网站。下面是代码:使用WebClient来ping一个网站

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

然而,抛出了以下错误:

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

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

+1

该网站是否使用哪种身份验证方法?另外,你只是想ping服务器启动?如果是这样,你可以做一个'HEAD'请求,并确保它不是一个500码或不响应连接。 – mellamokb

+0

所以,你想要ping(ICMP)或“ping”(只需确认服务器和应用程序已启动) – abatishchev

+1

您可以争辩说,您已经实现了您的目标.... – rene

回答

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