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. 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.
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.
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. > >
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. > >
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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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...
|
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...
|
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,...
|
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...
|
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...
|
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.
...
|
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...
|
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
|
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
|
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...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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...
|
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...
|
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
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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,...
|
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...
| |