Asp.net obtains the cookie value

Source: Internet
Author: User
Tags allkeys
The cookie value is displayed on the Response surface, and the token is as follows: Public   Partial   Class _ Default: system. Web. UI. Page
{
Protected   Void Page_load ( Object Sender, eventargs E)
{
// Retrieve the value in the cookie Scott 20080610
If (Request. Cookies. Count >   0 )
{

Httpcookiecollection hcccookie= NewHttpcookiecollection ();
//Obtain the cookie from the receiver.
Hcccookie=Request. Cookies;

// Get all keys
String [] Sarr = Hcccookie. allkeys;
Response. Write ( " Hcccookie. allkeys data volume " + Sarr. Length + " <Br> " );
Httpcookie HC;
Int I =   1 ;
Foreach ( String Scookie In Request. Cookies. allkeys)
{
Response. Write ( " The "   + I +   " Value: <br> " );
HC = Hcccookie [scookie];
Response. Write (convertutf8 (scookie) +   " Whether the sub-key is included: "   + HC. haskeys +   " <Br> " );

// Determines whether a Shard has a sub-key. If yes, the values are obtained by following the operator.
If (HC. haskeys)
{
Response. Write ( " The sub-key value is: <br> " );
Foreach ( String Svar In HC. values)
{
String Cookiesubkey = Convertutf8 (svar );
String Cookiesubvalue = Convertutf8 (HC [svar]);
Response. Write (cookiesubkey +   " : "   + Cookiesubvalue +   " <Br> " );
}
}
// When no sub-key exists, the value is obtained directly.
Else
{
Response. Write (convertutf8 (scookie) +   " Value: "   + Convertutf8 (HC. value) +   " <Br> " );
}

I++;
}
}
}

///   <Summary>
/// Convert content into utf8
///   </Summary>
///   <Param name = "asvalue"> </param>
///   <Returns> </returns>
Private   String Convertutf8 ( String Asvalue)
{
System. Text. Encoding encvalue = System. Text. encoding. getencoding ( " UTF-8 " );
// Httputility: provides methods for encoding and decoding URLs when processing Web requests.
String Sreturn = Httputility. urldecode (asvalue, encvalue );
Return Sreturn;
}
}

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.