Cookie Settings and Values | Solve the problem of garbled Chinese characters __ garbled

Source: Internet
Author: User

Cookie website Address http://plugins.jquery.com/cookie/


Cookies are a quick place to store "small chunks of data" in a browser. Settings, and read cookies, can be set up and read on the server side, and can be set up and read at the client

Every time the browser requests to the server, in addition to sending form parameters, all the cookies associated with the site are submitted to the server

Cookies are saved on the browser's side and the browser will submit the cookies to the server on each request, and the cookies returned from the server are updated back to the database so that the information can be stored in a cookie

The browser sends the requested form data and cookies to the server, and when the server is processed, returns the data to the browser, returning the data in addition to the normal HTML data, and returning the modified cookie (if there is a modified cookie, of course), The browser puts the cookie value in the new cookie to the local browser. That's why we can read and set cookies on the server side.

Cookies are divided into session cookies and persistent cookies
The session cookie is generally stored in the memory of the local browser process, and the cookie disappears as soon as the browser is closed.
Persistent cookies are saved in a folder on the local computer disk. That way, even if you shut down your browser or reboot your computer, you can guarantee that your cookie is still there.

By default, it is a session cookie. Cookies can set a validity period, if the expiration is not set is the session cookie, if the expiration date is set, it is a persistent cookie


Because cookies are saved by the browser, it is possible for different browsers to save the cookie path, the format of the data stored, the file size, and so on. For example, IE browser will be saved in a folder specified by IE browser, Firefox browser will be saved in a folder specified in Firefox, so the next cookie is not cross-browser. That is, cookie data cannot be shared between different browsers

When visiting a new Web site, the browser will decide whether to carry the coolie to the new site, depending on the cookie's settings. You can specify a valid domain name for the coolie when you write cookies. For example, baidu.com,goolgle.com, only when the cookie specifies the domain name, the browser will know that when the corresponding domain name of the site, will automatically carry the corresponding cookie


WebForm1.aspx page

<%@ Page language= "C #" autoeventwireup= "true" codebehind= "WebForm1.aspx.cs" inherits= "Webapplication3.webform1"% > <! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">  


WebForm1.cs Code

Using System;
Using System.Collections.Generic;
Using System.Linq;
Using System.Web;
Using System.Web.UI;

Using System.Web.UI.WebControls; namespace WebApplication3 {public partial class WebForm1:System.Web.UI.Page {protected void Page_Load (
        Object sender, EventArgs e) {} protected void Button1_Click (object sender, EventArgs e)

			{//Create cookie object HttpCookie cookie1 = new HttpCookie ("UserName", TextBox1.Text.Trim ()); Cookie1. Name = "UserName"; Sets the key//cookie1 for cookies. Value = "John"; Set the value of the cookie//Add the cookie just created to the collection of cookies in the Response object response this.
			
			RESPONSE.COOKIES.ADD (COOKIE1); Set the expiration time of the cookie cookie1. Expires.adddays (7);//cookie1.

            Expires = DateTime.Now.AddDays (7); Set a valid domain name for the cookie, that is, when you access the domain name, the browser will automatically carry the coolie. In general, you do not need to set//cookie1.

            Domain = "baidu.com"; Set a valid path for the cookie. /the site and directory, that is: access to the root of the site in any page, will carry the cookie,//cookie1. Path = "/"; Indicates that the cookie//cookie1 is only available if you visit a page in the site a directory.
            
			Path = "/a/"; When Cookie1. Secure =true said: Hypertext transfer security protocol for HTTPS will automatically bring the cookie defaults to false//that is: Cookie1. Secure =true indicates that SSL must be used, that is, the browser will automatically carry the cookie//cookie1 when it is HTTPS.
			
			
			
			
			Secure =true; Alternatively, set the cookie//response.setcookie (new HttpCookie ("UserName", TextBox1.Text) in this way;
            Set the value of the cookie on the service side} protected void button2_click (object sender, EventArgs e) { textbox2.text= request.cookies["UserName"].   Value; Get the value of the cookie on the service side}}

===========================================================


We can also not use the server directly in the customer list settings, and get cookies

 


Remember user name

Using System;
Using System.Collections.Generic;
Using System.Linq;
Using System.Web;
Using System.Web.UI;

Using System.Web.UI.WebControls; namespace WEBAPP {public partial class WebForm2:System.Web.UI.Page {System.Text.Encoding = System.
        Text.Encoding.GetEncoding ("Utf-8"); protected void Page_Load (object sender, EventArgs e) {if (! IsPostBack) {if (request.cookies["userName"]!= null) {/ /solve the user name is Chinese when garbled problem this. TextBox1.Text = Httputility.urldecode (request.cookies["UserName").
                Value, ENC); }} protected void button1_click (object sender, EventArgs e) {//Judge Che CkBox1 This check box that remembers the username is selected, and if selected, writes a username cookie to the client (this. checkbox1.checked) {///As client sends a username cookie HttpCookie cookie1 = new Httpco Okie ("UserName", Httputility.urlencode (this.
                TextBox1.Text, ENC)); Cookie1.
                Expires = DateTime.Now.AddDays (7); This.
            RESPONSE.COOKIES.ADD (COOKIE1);
                else {//If not selected, deletes the client's username cookie (if the key of the two cookie has the same name, the latter overrides the former) if (request.cookies["userName"]!= null) {HttpCookie HC = request.cookies["Usernam"
                    E "]; Hc.
                    Expires = DateTime.Now.AddDays (-7);
                RESPONSE.COOKIES.ADD (HC); }
            }

        }
    }
}


Chinese Coding Problem Reference article

String str = "China USA Germany Russia";  

 Converts a string into a byte array              
 byte[] bytes = System.Text.Encoding.UTF8.GetBytes (str);  

 Converts a byte array to a string  
 msg = System.Text.Encoding.UTF8.GetString (bytes);  


 File flow cannot manipulate strings, file stream operations are bytes  




 //----------------------------------- FileStream to write files-----------------------------------  


 //file stream using  

 //1. Create a file stream (Fswrite is a file stream, not a binary array)  
 FileStream fswrite = new FileStream (@ "D:\wowo.txt", FileMode.Create, FileAccess.Write);  

 2. Use file flow to perform operations to write data to files  
 string MSGB = "Today is a good day." Lalala ... ";  
 byte[] Bytesb = System.Text.Encoding.UTF8.GetBytes (MSGB);  





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.