Check if text file exists in ASP.NET

I need to check if a text file exists on a site in another domain. URL could be:

http://sub.somedomain.com/blah/atextfile.txt 

I need to do this from code. I am trying to use an HttpWebRequest object but don't know how to do this.

EDIT: I am looking for an easy way to do this, as I will execute this logic every few seconds

+4
source share
3 answers
 HttpWebRequest request = (HttpWebRequest)WebRequest.Create( "http://sub.somedomain.com/blah/atextfile.txt"); HttpWebResponse response = (HttpWebResponse)request.GetResponse(); if (response.StatusCode == HttpStatusCode.OK) { // FILE EXISTS! } response.Close(); 
+2
source

Something like this might work for you:

 using (WebClient webClient = new WebClient()) { try { using (Stream stream = webClient.OpenRead("http://does.not.exist.com/textfile.txt")) { } } catch (WebException) { throw; } } 
0
source

Source: https://habr.com/ru/post/1300483/


All Articles