Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Andrzejek on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

check for 404 page not found code 2

Status
Not open for further replies.

DotNetGnat

Programmer
Mar 10, 2005
5,548
IN
Guys,

I need to programmatically access/hit a URL and find out the response code before procedding any further. I need to check for 404 page not found error and validate user entry and show the error message.

Any suggestions for achieving this...

Thanks

-DNG
 
you will submit 1 request to check to see if the uri exists, and then a 2nd request, to the same uri, to actually do the work?
why not just submit the request once and handle the error if one occurs? If you really need to monitor the remote resource I would do this independent of the user's workflow. This keeps the user experience responsive.

in any case if you want to make a web request from code use WebRequest.

Jason Meckley
Programmer
Specialty Bakers, Inc.

faq855-7190
faq732-7259
 
Thanks Jason.

Can I use ajax or any other javascript libraries such as jquery to do this task?

I have a form ( a very big one) and one of the first user entry is the main part of the URL. I was hoping to do some client-side validation of the user entry to quickly return a invalid link message...

any suggestions...

-DNG
 
sure, the browser is just html. webforms can make that more difficult than your standard html view engine, but not impossible. as for validating on the client. I would keep that validation to simple data types: required, type checks, ranges, regex (like email address). i put the business validation on the server. This is only executed if all of the user input is valid.

in regards to a "very big form". I would break it up into smaller contexts and guide the user through the input process. like a wizard. step 1, step 2, step 3.

for validation implementation I use jquery Validator for the client, Castle.Validators to validate the user input at the server and a business rules engine for business logic validation. the business rules can be as simple as implmenting
Code:
interface IBusinessRule<T>
{
   bool IsBrokenBy(T item);
   string ErrorMessage {get;}
}
I can then create as many concrete implementations as I need. examples:
Code:
CustomerIsPreferred : IBusinessRule<Customer>
CustomerHasRecentActivityPreferred : IBusinessRule<Customer>
OrderWasCanceledPreferred : IBusinessRule<Order>

Jason Meckley
Programmer
Specialty Bakers, Inc.

faq855-7190
faq732-7259
 
I use this to check if an image exists before attempting to display it. It's part of a larger function that will give me a "no image" url should there not be an actual image out there. This is code I found laying around the web and take no credit for it. It shows some syntax for getting the response.

Code:
public bool CheckImageExist(string url)
            {
                bool returnValue = false;

                if (url.Length > 0)
                {
                    HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(url);
                    request.Method = "HEAD";
                    try
                    {
                        using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
                        {
                            returnValue = (response.StatusCode == HttpStatusCode.OK);
                        }
                    }
                    catch (WebException)
                    {
                        returnValue = false;
                    }
                }

                return returnValue;
            }

Good luck!

Mark
 
if the image is local your website, there is not need to issue another web request. you could check the physical file path and save the overhead of an http.

if the image is coming from a remote resource then this approach makes perfect sense.

Jason Meckley
Programmer
Specialty Bakers, Inc.

faq855-7190
faq732-7259
 
Jason,
Thanks for the tip. And you guessed it right... I'm getting images from our public website repository in the DMZ for display on the local intranet.

 
Thank you guys. WebResponse and WebRequest methods worked for me. Stars for you.

-DNG
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top