Asp.net C # check whether the URL is valid

Source: Internet
Author: User
Tags regular expression valid


Sometimes we need to check the validity of the website (URL) you enter,

The code is as follows: Copy code


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;
}

Check whether the URL entered by the user is valid. To check the URL, you can refer to the js regular expression to verify the URL format. There are many ways to check the URL validity. For example, you can use jQuery to check the URL,

This article provides a piece of code that uses C # to determine whether the website is valid.

The following describes how to check the URL validity:

The code is as follows: Copy code

Private bool UrlCheck (string strUrl)
{
If (! StrUrl. Contains ("http ://")&&! StrUrl. Contains ("https ://"))
    {
StrUrl = "http: //" + strUrl;
    }
Try
    {
HttpWebRequest myRequest = (HttpWebRequest) WebRequest. Create (strUrl );
MyRequest. Method = "HEAD ";
MyRequest. Timeout = 10000; // Timeout value: 10 seconds
HttpWebResponse res = (HttpWebResponse) myRequest. GetResponse ();
Return (res. StatusCode = HttpStatusCode. OK );
    }
Catch
    {
Return false;
    }
}

When using this method, you only need to input a Url to check the validity. Remember to add the System. Net namespace when using this method.

The code is as follows: Copy code

If (! UrlCheck ("http://www.111cn.net "))
{
// Delete operation
}

Yes. You can enter your website on this site, but some users do not know whether to test the website or what it is. You can enter a link that cannot be accessed at a glance. Therefore, it is necessary to check the URL entered by the user. Otherwise, too many invalid links will affect the weight of the website. More importantly, too many invalid links will affect the experience of other users, because no one wants to click an invalid link.

For more information, see jQuery Ajax,


The following five methods are used to execute the short form of a general Ajax request. jQuery. Ajax () should be used to process complicated ajax requests ().

1. load (url, [data], [callback])

Load the remote HTML file code and insert it into the DOM. The GET method is used by default, and the parameter is automatically converted to the POST method when passing parameters.

Upload url: The remote url to be loaded
Transmit data: key/value data sent to the server
Callback: the callback function when the load is successful.

The sample code is as follows:

The code is as follows: Copy code

// No parameter or callback function
$ ("# Showload"). load ("load.htm ");
// No Callback function
$ ("# Showload"). load ("load.htm", {"para": "para-value "});
$ ("# Showload"). load ("load.htm", {"para": "para-value "},
Function (){
// Process
})

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.