教程集 www.jiaochengji.com
教程集 >  脚本编程  >  Asp.net  >  正文 asp.net http 获取 cookie

asp.net http 获取 cookie

发布时间:2016-12-04   编辑:jiaochengji.com
教程集为您提供asp.net http 获取 cookie等资源,欢迎您收藏本站,我们将为您提供最新的asp.net http 获取 cookie资源

d |(.d{1,2}))(((d (d |(.d{1,2})))))";
423
424         public static string updateamountsql = "update orders set payamount = '{0}' where bigcode = '{1}'";
425         private static readonly regexoptions regexoptions = regexoptions.singleline | regexoptions.ignorecase | regexoptions.compiled;
426
427         public static string findvaluebyname(string strhtml, string elementname)
428         {
429             string strvalue = "";
430             regex regexregion = new regex(string.format(patternregion, elementname), regexoptions); //解析region的正则
431             try
432             {
433                 matchcollection mc = regexregion.matches(strhtml); //strhtml为要分析的网页代码
434                 foreach (match m in mc)
435                 {
436                     strvalue = m.groups["value"].value;//取匹配到的命名组的值
437                 }
438             }
439             catch (exception ex)
440             {
441                
442             }
443             return strvalue;
444         }
445         public static string changereturnhtml(string strhtml)
446         {
447             int begin = 0;
448             int end = 0;
449             while (begin != -1)
450             {
451                 begin = strhtml.toupper().indexof("<script".toupper());
452                 end = strhtml.toupper().indexof("</script>".toupper());
453                 if (begin != -1)
454                     strhtml = strhtml.remove(begin, end - begin 9);
455             }
456
457             strhtml = moveonload(strhtml);
458
459             return strhtml;
460         }
461         /// <summary>
462         /// 移除返回html中的onload事件
463         /// </summary>
464         /// <param name="inputstring">返回的html</param>
465         /// <returns>处理后的html</returns>
466         private static string moveonload(string inputstring)
467         {
468             int begin = 0;
469             int end = 0;
470             string _input = "";
471
472             while (begin != -1)
473             {
474
475                 if (inputstring.contains("onload"))
476                 {
477                     _input = inputstring.substring(inputstring.indexof("onload"));
478
479                     begin = _input.indexof("onload");
480                     end = _input.indexof("">");
481                     if (begin != -1 && end != -1)
482                     {
483                         _input = _input.substring(begin, end - begin 1);
484                         inputstring = inputstring.replace(_input, "");
485                     }
486                 }
487                 else
488                     begin = -1;
489             }
490             return inputstring;
491         }
492         public static string geturlcookies(string url, string cookiename)
493         {
494             uri uri = new uri(url);
495             cookiecontainer cc = cookiemanger.geturicookiecontainer(uri);
496             return cookiemanger.getcookiesstring(cc, uri);
497         }
498         public static string getsessionid(string cookies)
499         {
500             //match match = regex.match(cookies, @"(?<sessionid>jsessionid=([^;]*))", regexoptions.ignorecase);
501             try
502             {
503                 match match = regex.match(cookies, @"(?<sessionid>[^;,s]*sessionid=[^;]*)", regexoptions.ignorecase);
504                 if (match.success == true)
505                     return match.groups["sessionid"].value.trim();
506                 else
507                     return "";
508             }
509             catch (exception ex)
510             {
511                
512                 return "";
513             }
514         }
515         public static void seturlcookies(string url, string cookies)
516         {
517             if (cookies != null)
518             {
519                 string[] tmpcookies = cookies.split(";".tochararray());
520                 //stringbuilder sbcookiename = new stringbuilder();
521                 //stringbuilder sbcookievalue = new stringbuilder();
522
523                 uri uri = new uri(url);
524                 for (int i = 0; i < tmpcookies.length; i )
525                 {
526                     string[] cookie = tmpcookies[i].split('=');
527                     if (cookie.length != 2)
528                         continue;
529                     //sbcookiename.append(cookie[0]);
530                     //sbcookievalue.append(cookie[1]);
531
532                     internetsetcookie(url, cookie[0], cookie[1]/* ";expires=sun,22-feb-2099 00:00:00 gmt;"*/);
533                     //internetsetcookie(url, "","");
534                 }
535
536                 //internetsetcookie(url, sbcookiename, sbcookievalue);
537             }
538         }
539         public static string findbackpoint(string strhtml)
540         {
541             regex regs = new regex(regbackpoint);
542             return regs.match(strhtml).groups[3].value;
543         }
544         #endregion
545     }
546 }

    

</blockquote>

使用cookie实现单点登录
[1]cookie是由服务器端生成,发送给user-agent(一般是浏览器),浏览器会将cookie的key/value保存到某个目录下的文本文件内,下次请求同一网站时就发送该cookie给服务器(前提是浏览器设置为启用cookie)。cookie名称和值可以由服务器端开发自己定义,对于jsp而言也可以直接写入jsessionid,这样服务器可以知道该用户是否合法用户以及是否需要重新登录等。

在jsp中创建简单的cookie:   string cookiename="visittimes";   cookie cookie=new cookie(cookiename,"1");   cookie.setmaxage(10*60);//设置cookie存活期   cookie.addcookie(cookie);//将cookie写入客户端   在jsp中处理cookie数据的常用方法:   getdomain();返回cookie的域名.   getmaxage();返回cookie的存活时间   getname();返回cookie的名字   getpath();返回cookie适用的路径   getsecure();如果浏览器通过安全协议发送cookie将返回true值,如果浏览器使用标准协议刚返回false值   getvalue();返回cookie的值   getversion();返回cookie所遵从的协议版本   setcomment(string purpose);设置cookie的注释   setpath(string url);设置cookie的适用路径   setsecure(boolean flag);设置浏览器是否仅仅使用安全协议来发送cookie,例如使用https或ssl   setvalue(string newvalue);cookie创建后设置一个新的值   setversion(int v);设置cookie所遵从的协议版本.

您可能感兴趣的文章:
asp cookie 创建,取值,删除 教程
asp.net 操作cookie实例详解
c#如何写入和读取cookie
asp.net用cookie保存用户密码自动登录
asp.net操作cookie详解
asp.net Cookie用法举例
asp.net cookie操作实例学习
asp.net cookie详解
asp.net cookie的安全性
c#操作cookie的实现代码

[关闭]
~ ~