WinForm app needs to get statistics from a remotely hosted file

J

JT

Hi,

Is it possible to programmatically read the statistics of a file
hosted on a web server (not controlled by me)? I want to make files
available via RSS based on the suppliers giving me the name and URL of
their file. I don't want to require that they give me the file
length, but that's a required attribute of an RSS item's "enclosure"
element. The file length may change due to updates, so I'd like to be
able to determine the length at the time of the RSS creation.

My presumption is that since the file is available for download, that
anyone would have access to be able to read the file, but the
FileInfo, FileStream, and other classes that I've researched don't
provide access to URLs, that I can see.

Is this just something that's not allowed? Will I need to require the
provider to indicate and maintain the file length?

Thanks,

JT
 
N

Nicholas Paldino [.NET/C# MVP]

JT,

If the file is a static file, then you should be able to issue an HTTP
HEAD request and get the header information for that file (assuming you have
the url). The content length header will give you the length of the file,
in bytes.

You can use the HttpWebRequest and HttpWebResponse classes to issue this
request and get the content length header in the response.
 
J

JT

Hey Nicholas,

Thanks for the quick response. Here's what I tried:

try
{
System.Net.HttpWebRequest hwrTemp
=
(System.Net.HttpWebRequest)System.Net.WebRequest.Create(riTemp.Enclosure);
riTemp.FileType =
hwrTemp.Headers.Get("Content-Type");
riTemp.Length =
int.Parse(hwrTemp.Headers.Get("Content-Length"));
}
catch (Exception ex)
{
riTemp.FileType = "application/" +
riTemp.Enclosure.Substring(ExtensionStart);
riTemp.Length = 999999; //
this needs to somehow obtain the file length from the manufacturer's
site
}

ExtensionStart is the file extension portion of the Enclosure
property. Enclosure is the full URL to the file (http://
www.TheServerSite.com/ContainingFolder/TheFileIWant.zip)
What I'm getting is nulls for both headers.

Do I need to provide some sort of credentials for the request, even if
it's anonymous?

Thanks,

JT
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top