asp.net C # method to check whether URLs are valid

Source: Internet
Author: User
Tags regular expression

  This article mainly introduces ASP.net C # to check the URL is effective method, the need for friends can refer to the following

We sometimes need to check the user input Web site (URL) for validity,     code as follows: function Checkurl (str) {    var regurl = new RegExp ();     Regurl.compile ("^[a-za-z]+://[a-za-z0-9-_]+.[ a-za-z0-9-_%&?/.=]+$ ");     IF (! Regurl.test (str)) {        return false;    }     return true;     Not only from the format, but also to check whether the user entered the URL is really valid. Check the URL can refer to JS regular expression validation URL format, check the URL effective method There are many, for example, you can use jquery to check,   This article will give a section using C # to determine whether the site is valid code.   Below is a way to check the validity of URLs:     Code is as follows: private bool Urlcheck (string strurl) {    if (!strurl.contains ("http: ") &&!strurl.contains (" https://"))     {        strURL =" http://"+ strurl; &nb Sp  }     try     {        HttpWebRequest myrequest = (HttpWebRequest) webreque St. Create (strURL);         Myrequest.method = "Head";         myrequest.timeout = 10000;  //Timeout Time 10 sec         HTTPwebresponse res = (HttpWebResponse) myrequest.getresponse ();         return (res. StatusCode = = Httpstatuscode.ok);    }     catch     {        return false;    }}   &NB Sp When used, we only need to pass in the URL for the validity check, when using this method remember to add the System.Net namespace.     Code is as follows: if (! Urlcheck ("Http://www.jb51.net")) {   //delete operations}     Yes, in this site users can enter their own web site, but some users do not know is to test or what, A link that can be seen as being inaccessible is entered at a glance. So it is necessary for users to enter the URL to do a check, otherwise invalid links too many words will affect the weight of the site, more importantly, too many invalid links will affect the experience of other users, because who do not want to click an invalid link.   can refer to jquery  ajax,     The following 5 methods to perform a short form of a general Ajax request and use Jquery.ajax () when dealing with complex AJAX requests.   1.load (Url,[data],[callback])   Loads the remote HTML file code and inserts it into the DOM, using the Get method by default, which is automatically converted to post when the parameter is passed.  ◦url: Remote URL address to load ◦data: Key/value data to be sent to the server ◦callback: callback function when loading successfully   sample code as follows:     code is as follows://No parameters, no callback function $ (" #showload "). Load (" load.htm "); No callback function $ ("#showload"). Load ("load.htm", {"para": "Para-value"}); $ ("#showload"). Load ("load.htm", {"para": "Para-value"},     function () {       //processing    })    
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.