14416

Using WebClient to ping a web site

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?

Answer1:

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

Recommend

  • DownloadString returns a 404 Error: Site needs a User-Agent Header
  • Deserialize JSON without key
  • Detect site login url in Application_End
  • The length of the string exceeds the value set on the maxJsonLength property.c#
  • Httpd.conf multiple VirtualHost sections (was: Mono ASP.NET MVC AutoHosting issues)
  • Warning: mail(): Failed to Receive in
  • Masterpage CSS link can't resolve ~
  • Calling REST service with domain credentials from .NET 4.5 HttpClient
  • Whats wrong with this C shell script?
  • How to add headers on a 302 redirect in asp.net mvc
  • Is it always a good idea to import very specifically in Python?
  • ActionBar with appcompat library v7 (ava.lang.IllegalStateException: You need to use a Theme.AppComp
  • angularjs - ng-show doesn't update class when $interval triggers
  • docker-compose: connection refused between containers, but service accessible from host
  • How does `std::terminate` know to treat `std::exception`s specially?
  • Handling exceptions in a class library enveloping a device driver
  • Why does .addView throw this parent/child exception?
  • API (curl)Command to Approve a promoted build Job in Jenkins
  • Catch 500 internal server error with System.WebClient
  • Can my app be notified when another application starts/stops playing audio?
  • Android Database Error - getWriteableDatabase
  • perl, mysql - fasting way to upload a csv file into mysql?
  • Installing iPhone App to iPhone
  • SignalR .NET Client Invoke throws an exception
  • Installing Hadoop, Java Exception about illegal characters at index 7?
  • Is there any way to access browser form field suggestions from JavaScript?
  • Azure Cloud Service Web Role web pages do not load
  • Resize panoramic image to fixed size
  • Volusion's generic SQL folder, functionality
  • Arrays break string types in Julia
  • bootstrap to use multiple ng-app
  • How to get icons for entities from eclipse?
  • Hits per day in Google Big Query
  • How do you join a server to an Active Directory (domain)?
  • coudnt use logback because of log4j
  • FormattedException instead of throw new Exception(string.Format(…)) in .NET
  • Turn off referential integrity in Derby? is it possible?
  • Linking SubReports Without LinkChild/LinkMaster
  • XCode 8, some methods disappeared ? ex: layoutAttributesClass() -> AnyClass
  • JaxB to read class hierarchy