In this article I will explain how to solve the following error (exception) occurring inside the DownloadString method of WebClient class in .Net using C# and VB.Net.
System.Net.WebException: ‘The remote server returned an error: (404) Not Found’.
 
 
Exception
In the following code snippet, the file is being read using the DownloadString method of WebClient class.
C#
ServicePointManager.Expect100Continue = true;
ServicePointManager.SecurityProtocol = (SecurityProtocolType)3072;
string contents = (new WebClient()).DownloadString("https://raw.githubusercontent.com/aspsnippets/test/master/ReadMe.txt");
 
VB.Net
ServicePointManager.Expect100Continue = True
ServicePointManager.SecurityProtocol = CType(3072, SecurityProtocolType)
Dim contents As String = (New WebClient()).DownloadString("https://raw.githubusercontent.com/aspsnippets/test/master/ReadMe.txt")
 
But it raises the following exception:-
System.Net.WebException: ‘The remote server returned an error: (404) Not Found’.
 
.Net WebClient Exception (Error): (404) Not Found
 
 
Reason
This exception occurred since the file is not exists in the specified URL.
 
 
Solution
The solution is to make sure that the file is present in the URL.
You can check the URL in the browser to verify the file.