473,387 Members | 1,512 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Download file

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.
Nov 16 '05 #1
5 1606
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]
- mv*@spam.guard.caspershouse.com

"Leon Habinsky" <_n**************@hotmail.com> wrote in message
news:ut**************@TK2MSFTNGP12.phx.gbl...
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.

Nov 16 '05 #2
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 [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> wrote in
message news:%2****************@TK2MSFTNGP12.phx.gbl...
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]
- mv*@spam.guard.caspershouse.com

"Leon Habinsky" <_n**************@hotmail.com> wrote in message
news:ut**************@TK2MSFTNGP12.phx.gbl...
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.


Nov 16 '05 #3
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]
- mv*@spam.guard.caspershouse.com

"Leon Habinsky" <_n**************@hotmail.com> wrote in message
news:%2****************@TK2MSFTNGP10.phx.gbl...
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 [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> wrote
in
message news:%2****************@TK2MSFTNGP12.phx.gbl...
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]
- mv*@spam.guard.caspershouse.com

"Leon Habinsky" <_n**************@hotmail.com> wrote in message
news:ut**************@TK2MSFTNGP12.phx.gbl...
> 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.
>
>



Nov 16 '05 #4
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 [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> wrote in
message news:ut**************@TK2MSFTNGP15.phx.gbl...
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]
- mv*@spam.guard.caspershouse.com

"Leon Habinsky" <_n**************@hotmail.com> wrote in message
news:%2****************@TK2MSFTNGP10.phx.gbl...
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 [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> wrote
in
message news:%2****************@TK2MSFTNGP12.phx.gbl...
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]
- mv*@spam.guard.caspershouse.com

"Leon Habinsky" <_n**************@hotmail.com> wrote in message
news:ut**************@TK2MSFTNGP12.phx.gbl...
> 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.
>
>



Nov 16 '05 #5
Leon Habinsky wrote:
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,

--
Joerg Jooss
www.joergjooss.de
ne**@joergjooss.de
Nov 16 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Brandon Walters | last post by:
I wrote a file download module for my website. The reason for the file download module is that my website downloads work on a credit based system. So I need to keep track of and limit daily...
0
by: Buddy Ackerman | last post by:
I am trying to implment a file download via a link such that when clicked, instead of starting the default application for that type of file the user will be presented with a download dialog...
0
by: Rhys666 | last post by:
Basically I have a link that opens my download page and the querystring identifies the type of 'template' Excel spreadsheet has asked to download. The download page reads the querystring,...
4
by: Nathan Sokalski | last post by:
I want to give visitors to my site the option of downloading a generated ..txt file by clicking a button. I know how to generate text files, but how do I cause the browser to pop up one of those...
1
by: a.r.austin | last post by:
Hello, I am trying to download a few files one after another from a remote server. Problem is that I don't know how to, or if I am able at all, set a time out for download. I don't want to time...
3
by: tshad | last post by:
I have a function that downloads a file to the users computer and it works fine. The problem is that I then want the program to rename the file (file.move) to the same name plus todays date. ...
16
by: matt | last post by:
I have used some free code for listing files for download, but I want to send an email to the administrator when the file has been downloaded. I have got some code in here that does it, but it will...
0
by: kain razial | last post by:
ANYOTHER NEEDS ALSO CONTACT ME.I WILL HELP YOU TO GET THAT TEKLA XSTEEL V 12 DOWNLOAD LINK. http://www.sendspace.com/file/zem32r
0
by: kain razial | last post by:
ANYOTHER NEEDS ALSO CONTACT ME.I WILL HELP YOU TO GET THAT TEKLA XSTEEL V 12 DOWNLOAD LINK. http://www.sendspace.com/file/zem32r
1
KevinADC
by: KevinADC | last post by:
Note: You may skip to the end of the article if all you want is the perl code. Introduction Many websites have a form or a link you can use to download a file. You click a form button or click...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.