C # cookie use instance

Source: Internet
Author: User
Some time ago, when performing the online examination part of the examination system, you need to record the answers to the questions when the examinee is online and refer to others' practices. There are two ways to solve this problem: 1. Create a table in the database to save the answers to the answers provided by the examinee. After the examinee submits the paper, insert the data in the table that temporarily saves the answers to the examinee into the formal table that records the examinee's answers; the second is not to use tables in the database to store answers to questions before the exam is handed in. Instead, the answer information before the exam is saved in the file, after the examinee submits the paper, the information in the file that records the examinee's answer information is written into the database. The advantage of the first approach is that the answer situation in the course of answering questions can ensure better correctness. The disadvantage is that the possibility of Data Writing cannot be caused by an error in the connection with the database. In addition, another drawback is that there are many candidates for the online examination. If you perform operations on the database every time you answer a question, frequent database access and operations will increase the pressure on the database, it may cause serious consequences. The advantage of the second approach is to avoid the disadvantages of the first approach. The disadvantage is that the answer data during the exam is very important and the record may be tampered with in the document. After considering the environment and actual situation of the examination system, the customer decided to adopt the second approach. In view of the disadvantages of the second approach, because the network environment is an internal LAN and the input and output devices of the machines used by the examinee are limited, the disadvantages are not enough to predict the probability of occurrence. In addition, the customer repeatedly asked the online examination to minimize all factors that affect the normal conduct of the examination, minimize the access to the database, and decide to adopt the second approach after the trade-off. Starting from the second idea, I used the cookie file to temporarily Save the examinee's answer information. Since I have never tried this method before, I first went to the Internet to find a large amount of information. It took about two or three days to implement the group function. After a lot of detours, we recorded the process to avoid repeating the same mistakes in the future.
This method is used to write the examinee's answer information stored in the cookie file to the database until the examination is submitted. The optimization and reasonable process are as follows: log on to the application console, click Create cookie file, and then click Submit (upload the obtained data to the server in XML format for later use ). In this process, operations on cookie files are important, because the information recorded is crucial, when updating the cookie file, you must consider whether the question answered by the examinee is recorded in the cookie file or is answered for the first time, then, add the answer information to the original cookie file. If you have answered the question before and modify it now, you must find the information for the corresponding questions saved in the cookie file and update it, because every time you answer a question, you must update the cookie file. Therefore, you must update the cookie file as a method, which can be called repeatedly.
All the ideas and steps are clear, and the code is a matter of course. The following is the most important part of the code for updating cookies:
/// <Summary>
/// Update the cookie file content
/// </Summary>
Private void updatecookie ()
{
// Add answer information to the cookie set
Int c_timulx = convert. toint32 (viewstate ["c_timulx"]); // question type
String c_bianhao = convert. tostring (viewstate ["c_bianhao"]); // question No.
String cookieid = viewstate ["cookieid"]. tostring ();
String Daan = This. getdaan (c_timulx); // obtain the examinee's answer
// Obtain the content in the original cookie file
String oldcookievalue = "";
If (request. Cookies [cookieid]. value! = NULL)
{
Oldcookievalue = request. Cookies [cookieid]. value;
}
// If the answer to the current question already exists in the original cookie file, retrieve the information of the current question in the original cookie file and modify it.
String newcookievalue = ""; // content of the new cookie file
String newanswer = "|" + c_timulx.tostring () + "," + c_bianhao; // the content of the current question in the original cookie file
If (oldcookievalue. Contains (newanswer ))
{
// Delete the original cookie file
Httpcookie oldcookie = request. Cookies [cookieid];
Oldcookie. expires = datetime. Now. addhours (-1 );
Response. Cookies. Add (oldcookie );
// Assemble the content of the new cookie file
String [] temp = oldcookievalue. Split ('| ');
Foreach (string s in temp)
{
String [] tempdaan = S. Split (',');
String element1 = tempdaan [0]; //
String element2 = tempdaan [1];
String element3 = tempdaan [2];
If (c_timulx.tostring () = element1 & c_bianhao = element2) // edit the question that has been saved in the cookie
{
Newcookievalue = oldcookievalue. replace ("|" + element1 + "," + element2 + "," + element3, "|" + c_timulx.tostring () + "," + c_bianhao + ", "+ Daan );
}
}
}
Else // if the answer information for the current question does not exist in the original cookie file, add the answer for the current question to the original cookie file
{
// Delete the original cookie file
Httpcookie oldcookie = request. Cookies [cookieid];
Oldcookie. expires = datetime. Now. addhours (-1 );
Response. Cookies. Add (oldcookie );
// Assemble the content of the new cookie file
Newcookievalue = oldcookievalue + "|" + c_timulx.tostring () + "," + c_bianhao + "," + Daan;
}
// Create a new cookie file and create a cookie object
Httpcookie objcookie = new httpcookie (cookieid, newcookievalue );
// Set the lifecycle of the cookie file
Datetime now = datetime. now;
Timespan Ts = new timespan (2, 0, 0); // set the lifecycle to 2 hours
Objcookie. expires = now + ts;
// Set the cookie to use SSL Transmission
// Objcookie. Secure = true;
Response. Cookies. Add (objcookie );
}

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.