C # operation URL parameters

Source: Internet
Author: User
Code
/* ----------------------------------------
* Function: C # operation URL parameters
* Author: robot. h
* Created: 20081117
--------------------------------------- */
Using System;
Using System. Text. regularexpressions;
Using System. runtime. remoting. contexts;
Class App
{
Static   Void Main ()
{
// Generate random
Random Rand =   New Random (environment. tickcount );
Int Num = Rand. Next ( 1 , 10000 );

/*
* Http://news.sina.com.cn
* Http://news.sina.com.cn ? Page = 1
* Http://news.sina.com.cn ? Page = 1 & TS = 1341 &&
* Http://news.sina.com.cn ? TS = 14321 & page = 1? &
* Http://news.sina.com.cn ? U = 2 & TS = 14321 & page = 1 &&
*/
String URL =   " Http://news.sina.com.cn? U = 2 & TS = 14321 & page = 1 " ;
String Paramtext =   " TS " ;
String Paramvalue = Num. tostring ();
Console. writeline (buildurl (URL, paramtext, paramvalue ));
Console. Read ();

}
// If there is a key value in the URL, replace it with the value. If there is no key, append it.
Public   Static   String Buildurl ( String URL, String Paramtext, String Paramvalue)
{
RegEx Reg =   New RegEx ( String . Format ( " {0} = [^ &] * " , Paramtext), regexoptions. ignorecase );
RegEx reg1 =   New RegEx ( " [&] {2 ,} " , Regexoptions. ignorecase );
String _ URL = Reg. Replace (URL, "" );
// _ Url = reg1.replace (_ URL ,"");
If (_ URL. indexof ( " ? " ) =   - 1 )
_ URL + =   String . Format ( " ? {0} = {1} " , Paramtext, paramvalue ); // ?
Else
_ URL + =   String . Format ( " & {0 }={ 1} " , Paramtext, paramvalue ); // &
_ URL = Reg1.replace (_ URL, " & " );
_ URL = _ URL. Replace ( " ? & " , " ? " );
Return _ URL;
}

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.