记录一下今天用到的。
js操作cookie:
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 == 0) break;
}
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));
}
//设定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 == 0) break;
}
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:
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);
}
}
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不行)
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;
}
}
}
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>