js操作cookie;js的setInterval;C#获取指定页面的内容;Ajax.dll的使用

记录一下今天用到的。

js操作cookie:

ContractedBlock.gif ExpandedBlockStart.gif Code
//设定Cookie值
function SetCookie(name, value){
    
var expdate = new Date();
    
var argv = SetCookie.arguments;
    
var argc = SetCookie.arguments.length;
    
var expires = (argc > 2? argv[2] : null;
    
var path = (argc > 3? argv[3] : null;
    
var domain = (argc > 4? argv[4] : null;
    
var secure = (argc > 5? argv[5] : false;
    
if (expires != null) expdate.setTime(expdate.getTime() + (expires * 1000));
    document.cookie 
= name + "=" + escape(value) + ((expires == null? "" : ("; expires=" + expdate.toGMTString()))
        
+ ((path == null? "" : ("; path=" + path)) + ((domain == null? "" : ("; domain=" + domain))
        
+ ((secure == true? "; secure" : "");
}
//删除Cookie
function DelCookie(name){
    
var exp = new Date();
    exp.setTime(exp.getTime() 
- 1);
    
var cval = GetCookie(name);
    document.cookie 
= name + "=" + cval + "; expires=" + exp.toGMTString();
}
//获得Cookie的原始值
function GetCookie(name) {
    
var arg = name + "=";
    
var alen = arg.length;
    
var clen = document.cookie.length;
    
var i = 0;
    
while (i < clen) {
        
var j = i + alen;
        
if (document.cookie.substring(i, j) == arg)
            
return GetCookieVal(j);
        i 
= document.cookie.indexOf(" ", i) + 1;
        
if (i == 0break;
    }
    
return null;
}
//获得Cookie解码后的值
function GetCookieVal(offset) {
    
var endstr = document.cookie.indexOf(";", offset);
    
if (endstr == -1)
        endstr 
= document.cookie.length;
    
return unescape(document.cookie.substring(offset, endstr));
}

 

Js的setInterval:

ContractedBlock.gif ExpandedBlockStart.gif Code
function AppCAValid(appIndex){
   document.getElementById(
"caframe").src = GetCookie("CAVPage");
   document.getElementById(
"msg").innerHTML = "正在验证中"
   theTimer 
= setInterval("CheckCAValid()",5000);
}

function CheckCAValid(){
   
//GetPage.IsValidateError:Ajax方法抓取验证页面的内容,根据内容判断验证是否通过
   var result = GetPage.IsValidateError(GetCookie("CAVPage")).value; 
   
if(result == "1"){   
       document.getElementById(
"msg").innerHTML = "登录失败,请重新登录!";
       clearInterval(theTimer);
   }   
}

 

C#获取指定WEB页面的内容:(必须基于HTTP,HTTPS不行)

 

ContractedBlock.gif ExpandedBlockStart.gif Code
namespace Components
{
    
/// <summary>
    
/// GetPage 的摘要说明
    
/// </summary>
    
    
public class GetPage
    {
        
public GetPage()
        {
            
//
            
// TODO: 在此处添加构造函数逻辑
            
//
        }

        
/// <summary>
        
/// 分析CA认证页面的内容,判断登录是否成功
        
/// </summary>
        
/// <param name="Url">CA认证页面的URL</param>
        
/// <returns>成功返回'1',不成功返回'0'</returns>
        [Ajax.AjaxMethod]
        
public string IsValidateError(string Url) 
        {             
            
string content = GetHttp(Url,"GET","utf-8");
            
if(content.IndexOf("验证失败"> -1
            {
                
return "1"
            }
            
else
            {
                
return "0";
            }
        }

        
/// <summary>
        
/// <param name="Url">URL路径</param>
        
/// <param name="PostType">POST类型 GET/POST</param>
        
/// <param name="codes">编码GB2312 / utf-8</param>
        
/// <returns></returns>
        
/// </summary>
        public string GetHttp(string Url ,string PostType,string codes)
        {
            
string StrHttp= "";
            
try
            {
                
if (PostType == "POST")
                {
                    
string[] TmpUrl = Url.Split('?');
                    
string PostStr = TmpUrl[TmpUrl.Length - 1];
                    
byte[] requestBytes = System.Text.Encoding.Default.GetBytes(PostStr);
                    HttpWebRequest httpReq 
= (HttpWebRequest)WebRequest.Create(TmpUrl[0]);
                    httpReq.Timeout 
= 10000;//设置超时值2秒
                    httpReq.Method = "POST";
                    httpReq.ContentType 
= "application/x-www-form-urlencoded";
                    httpReq.ContentLength 
= requestBytes.Length;
                    Stream requestStream 
=httpReq.GetRequestStream();
                    requestStream.Write(requestBytes,
0,requestBytes.Length);
                    requestStream.Close();

                    HttpWebResponse res 
= (HttpWebResponse)httpReq.GetResponse();
                    StreamReader sr 
= new StreamReader(res.GetResponseStream(),System.Text.Encoding.GetEncoding(codes));
                    StrHttp 
=sr.ReadToEnd();
                    sr.Close();
                    res.Close();
                    sr 
= null;
                    res 
= null;
                }
                
else
                {
                    HttpWebRequest httpReq 
= (HttpWebRequest)WebRequest.Create(Url);//HttpWebRequest 类对 WebRequest 中定义的属性和方法提供支持',也对使用户能够直接与使用 HTTP 的服务器交互的附加属性和方法提供支持。
                    httpReq.ContentType = "application/x-www-form-urlencoded";
                    
//httpReq.Headers.Add("Accept-Language", "zh-cn");
                    httpReq.Timeout = 10000//设置超时值2秒
                    httpReq.Method = "GET";
                    HttpWebResponse httpResq 
= (HttpWebResponse)httpReq.GetResponse();
                    StreamReader reader 
= new StreamReader(httpResq.GetResponseStream(), System.Text.Encoding.GetEncoding(codes));//如是中文,要设置编码格式为“GB2312”。
                    string respHTML = reader.ReadToEnd(); //respHTML就是页面源代码
                    StrHttp = respHTML;
                    httpResq.Close();
                    httpResq 
= null;
                }
            }
            
catch(Exception ex)
            {
                StrHttp 
= ex.Message;
            }
            
return StrHttp;
        }
    }
}

 

setInterval中使用AjaxPro来实现Ajax时要具备以下条件:

1.项目要引用Ajax.dll

2.C#的GetPage类的Ajax方法IsValidateError()前加[Ajax.AjaxMethod]

3.实现Ajax的Web页面:引用GetPage类所在命名空间;Page_Load中声明Ajax.Utility.RegisterTypeForAjax(typeof(Components.GetPage));

4.Web.config的<system.web>中加:
   <httpHandlers>
    <add verb="POST,GET" path="ajax/*.ashx" type="Ajax.PageHandlerFactory, Ajax" />
   </httpHandlers>

posted on 2009-06-12 10:30  大丫洋芋 阅读( ...) 评论( ...) 编辑 收藏

转载于:https://www.cnblogs.com/huadd/archive/2009/06/12/1501901.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值