How to test if a URL exists from a .NET program?
How can you tell if a URL exists from a .NET program? I tried using the WebBrowser control with the Navigate method. It works OK except when the file is a PDF. In that case, it invokes Adobe Reader to pop up the PDF. I don't actually want to bring up a PDF, I just want to know if it exists. It won't work too well to keep popping up PDF files. Do you just remove the file type association in the "programs" option in Internet Options / Programs? Have you guys done this with straight .NET? If not, how would you do it with screen-scrapes and and a .NET program?
I'm sorry that I don't
I'm sorry that I don't understand the question.
Do you want to confirm that a URL is valid by calling a scrape through VB.NET?
Gary,If you're talking about
Gary,
If you're talking about the same scenario as in your earlier post and if I'm understanding what you're needing then, I believe, screen-scraper could handle this. Are you manually constructing the URL versus extracting the link from the page? Is this perhaps why you're wanting to verify that it is a valid URL before requesting it? If so, then you have to do a bit of dancing just to determine whether or not you'll get a 404 without downloading the entire file.
The script below will first indicate if the URL is valid and if it is return the contentType. The contentType is what your browser looks at to know whether or not to load Acrobat or prompt you to download a file, for example.
It's likely that .Net has a way to do this that may not be so verbose, but if you create a script using the following, pass the script your URL as a session variable and check the value of the session variable "CONTENT_TYPE".
.NET code that checks if a URL exists
Thanks for the code. Is that interpreted Java or JavaScript? I checked .NET forums for code that might work within a .NET 3.5 program. I found a couple of things and pieced them together into something that appears to work. I'll test it further to make sure that it indeed works for all cases. Here it is:
Gary, That looks like it
Gary,
That looks like it would work. You would need to pass the URL back and forth from screen-scraper to your app if you were to have .Net do the URL verification. That can be done. Depending on whether the site requires you to maintain a session state you may need to handle that exchange keeping that in mind.
-Scott