Download file

  • Thread starter Thread starter Leon Habinsky
  • Start date Start date
L

Leon Habinsky

Hi guys,

I need to download a file over the Internet and I use the HttpWebRequest
class to accomplish this task. My problem is that, I need to know if the
file exists before issuing the GetResponse() call , because it throws
WebException. I can of course handle the exception but it's not the best way
for me to handle the logic of the my program. Is there any other ways to do
it?

Thanks in advance.
Leon.
 
Leon,

You should be able to call GetResponse and get the HttpWebResponse from
it without an exception. Once you have it, you should be able to check the
StatusCode property to see if the file exists or not (if it doesn't, I
believe you will get a status code in the 400's).

Hope this helps.
 
Nicholas,
Thank you for the prompt response. That is exactly what I've tried doing,
but as soon as I call httpWebResponse = (HttpWebResponse)
httpRequest.GetResponse()
with an invalid URL it throws an exception. Maybe I'm doing something wrong
here?
BTW I'm using #develop for this.

Thanks
Leon.


Nicholas Paldino said:
Leon,

You should be able to call GetResponse and get the HttpWebResponse from
it without an exception. Once you have it, you should be able to check the
StatusCode property to see if the file exists or not (if it doesn't, I
believe you will get a status code in the 400's).

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Leon Habinsky said:
Hi guys,

I need to download a file over the Internet and I use the HttpWebRequest
class to accomplish this task. My problem is that, I need to know if the
file exists before issuing the GetResponse() call , because it throws
WebException. I can of course handle the exception but it's not the best
way
for me to handle the logic of the my program. Is there any other ways to
do
it?

Thanks in advance.
Leon.
 
Leon,

Is the URL that you are passing a valid url, or not? If the URL is
invalid, then the exception will be thrown. However, if the URL is a valid
URL, but the resource doesn't exist, then the server should return a status
code.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Leon Habinsky said:
Nicholas,
Thank you for the prompt response. That is exactly what I've tried doing,
but as soon as I call httpWebResponse = (HttpWebResponse)
httpRequest.GetResponse()
with an invalid URL it throws an exception. Maybe I'm doing something
wrong
here?
BTW I'm using #develop for this.

Thanks
Leon.


in
message news:%[email protected]...
Leon,

You should be able to call GetResponse and get the HttpWebResponse from
it without an exception. Once you have it, you should be able to check the
StatusCode property to see if the file exists or not (if it doesn't, I
believe you will get a status code in the 400's).

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Leon Habinsky said:
Hi guys,

I need to download a file over the Internet and I use the
HttpWebRequest
class to accomplish this task. My problem is that, I need to know if
the
file exists before issuing the GetResponse() call , because it throws
WebException. I can of course handle the exception but it's not the
best
way
for me to handle the logic of the my program. Is there any other ways
to
do
it?

Thanks in advance.
Leon.
 
Nicholas,

Let say it works for the file "http://localhost/MyStuff/MyFile.exe" which
exists, but throws an exception for the file
"http://localhost/MyStuff/MyFile2.exe" which dosn't exist.

Thanks.
Leon.
Nicholas Paldino said:
Leon,

Is the URL that you are passing a valid url, or not? If the URL is
invalid, then the exception will be thrown. However, if the URL is a valid
URL, but the resource doesn't exist, then the server should return a status
code.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Leon Habinsky said:
Nicholas,
Thank you for the prompt response. That is exactly what I've tried doing,
but as soon as I call httpWebResponse = (HttpWebResponse)
httpRequest.GetResponse()
with an invalid URL it throws an exception. Maybe I'm doing something
wrong
here?
BTW I'm using #develop for this.

Thanks
Leon.


in
message news:%[email protected]...
Leon,

You should be able to call GetResponse and get the HttpWebResponse from
it without an exception. Once you have it, you should be able to check the
StatusCode property to see if the file exists or not (if it doesn't, I
believe you will get a status code in the 400's).

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Hi guys,

I need to download a file over the Internet and I use the
HttpWebRequest
class to accomplish this task. My problem is that, I need to know if
the
file exists before issuing the GetResponse() call , because it throws
WebException. I can of course handle the exception but it's not the
best
way
for me to handle the logic of the my program. Is there any other ways
to
do
it?

Thanks in advance.
Leon.
 
Leon said:
Hi guys,

I need to download a file over the Internet and I use the
HttpWebRequest class to accomplish this task. My problem is that, I
need to know if the file exists before issuing the GetResponse()
call , because it throws WebException. I can of course handle the
exception but it's not the best way for me to handle the logic of the
my program. Is there any other ways to do it?

No. Any error level HTTP status code is reported as WebException. Why should
your code have trouble dealing with that?

Cheers,
 
Back
Top