1

As you all know there is many file host websites, is there a way to process the http link of a of file on one of those sites and retrieve a result if the file exists or if the http link even exists or not. I know that maybe some of those file host websites uses their own APIs but i want a more generic way.

Edit:

So as i understand there is no file on a server, it's just that i have to read the response and read it properly. I want to ask another thing, what about redirection, does that mean if i got the response of a link that redirects to other link, i will get the final target from the response ?

ykh
  • 1,775
  • 3
  • 31
  • 57
  • 2
    You can use `HttpRequest` to make a request and read a response. Whether the "file" exists depends on what the server sends as a response. There really is no such thing as "files" in HTTP, only requests and responses (which contain headers and content). – David Jul 09 '12 at 11:14

2 Answers2

1

You can find out if a file exist using the exists method:

bool System.IO.File.Exists(string path)

///

in order to find out if a file exist on a remove server you can try this:

WebRequest request;
WebResponse response;
String strMSG = string.Empty;
request = WebRequest.Create(new Uri(“http://www.yoururl.com/yourfile.jpg”));
request.Method = “HEAD”;

try
{
    response = request.GetResponse();
    strMSG = string.Format(“{0} {1}”, response.ContentLength,         response.ContentType);
}
catch (Exception ex)
{
   //In case of File not Exist Server return the (404) Error
    strMSG = ex.Message;
}

see this:

03Usr
  • 3,335
  • 6
  • 37
  • 63
  • 1
    This assumes the server will respond with a 404 error, or any error at all. It might not, we don't know. Also, this isn't very good exception handling. There could be more useful information in the `Exception` object that you're just throwing away. – David Jul 09 '12 at 11:41
1

If I understand you correctly, you're trying to tell if a given URL has content.

Use the

WebClient

class.

Call the url, if you receive a 200, you're good to go. A 404 exception or similar probably means the link is no good.

Or, even better way to do this is to do a HEAD http request. See here for more info on that.

Community
  • 1
  • 1
Nik
  • 2,718
  • 23
  • 34