C# Program to check if a file exists at a particular URL and download the content

Rajnilari2015
Posted by Rajnilari2015 under C# category on | Points: 40 | Views : 453
The below program will do so

using System;
using System.Net;

namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
string fileLocation = @"http://TestWebAPI/SampleText/sample.txt";
var serverResponse = GetServerResponse(fileLocation);
Console.WriteLine("URL Validity : {0} \n Server Response : {1}", serverResponse.Item1, serverResponse.Item2);
Console.ReadKey();
}

private static Tuple<bool,string> GetServerResponse(string fileLocation)
{
bool isValidURL = true;
string data = string.Empty;
using (WebClient client = new WebClient())
{
try
{
data = client.DownloadString(fileLocation);
}catch(WebException weX){
isValidURL = !isValidURL;
data = weX.Message;
}
}
return Tuple.Create<bool, string>(isValidURL, data);
}
}
}

Comments or Responses

Login to post response