Asp函数集合+Web代理(JavaScript版)

2006-07-25 22:44:07

用JavaScript作为Asp服务器端脚本语言写的一个通用函数集合文件 版本 0.1 作者 LlinZzi 发布日期 2006年3月24日 功能 数据库:数据库连接(包括Sqlserver和Access),数据库查询,关闭连接 字符过滤:html代码转化,防注入 增强功能:自动输出数据库表格 [code]<%@LANGUAGE="JAVASCRIPT" CODEPAGE="65001"%> <% Response.Charset="utf-8"; %> <% Session.CodePage=65001; %> <% //程序全局变量和初始化 //sql_connection("llinzzi","sa","llinzzi"); //access_connection("llinzzi.mdb"); //conn定义为全局变量 var conn; //定义access数据库连接 function access_connection(database){ try { conn = Server.CreateObject("ADODB.Connection"); conn.open("driver={Microsoft Access Driver (*.mdb)};DBQ="+Server.MapPath(""+database+"")); } catch(e){ Response.Write(e.description); } } //定义sql数据库连接 function sql_connection(database,username,password){ try { conn = Server.CreateObject("ADODB.Connection"); conn.open("Provider=SQLOLEDB;Server=127.0.0.1;Database="+database+";UID="+username+";PWD="+password+""); } catch(e){ Response.Write(e.description); } } //定义connection数据库关闭 function connection_close(){ try{ conn.close; conn=null; rs=null; } catch(e){ Response.Write(e.description); } } //定义sql数据库操作 function get_data(sql){ try{ var rs = Server.CreateObject("ADODB.RecordSet"); rs.open(sql,conn,3,2); return(rs); } catch(e){ Response.Write(e.description); } } //数据过滤 function conversion_content(content){ try{ content=content.replace(/>/g,">"); content=content.replace(/</g,"<"); content=content.replace(/\"/g,"""); content=content.replace(/\'/g,"´"); content=content.replace(/\r/g,"<br />"); content=content.replace(/\s/g," "); return(content); } catch(e){ Response.Write(e.description); } } function out_table(sql){ var rs = get_data(sql); out_string="<table width=\"384\" border=\"1\" cellspacing=\"0\" cellpadding=\"0\">\n"; out_string+="\t<tr>\n"; for(var i=1;i<rs.Fields.Count;i++){out_string+="\t\t<td>"+rs(i).name+"</td>\n"} out_string+="\t</tr>\n"; while(!rs.eof && !rs.bof){ out_string+="\t<tr>\n"; for(var i=1;i<rs.Fields.Count;i++){out_string+="\t\t<td><input type=\"text\" name=\""+rs(i).name+"\" value=\""+rs(i)+"\"></td>\n"} out_string+="\t</tr>\n"; rs.MoveNext; } out_string+="</table>\n"; Response.Write(out_string); connection_close(); } %> [/code] asp写Web代理软件,可以用作突破网关限制等,主要是为了配合Ajax使用的(因Firefox存在跨域访问的问题) 版本 0.1 作者 LlinZzi 功能 判断网站编码,支持任何语言 超连接自动转换 附带一个远程下载图片的函数 未解决问题,目前只能用来代理网站代码,图片等其他数据的代理转发尚未完成。 [code] <%@LANGUAGE="JAVASCRIPT" CODEPAGE="65001"%> <%Response.Charset = "utf-8"%> <% Server.ScriptTimeout=9999999; var Surl = String(Request.QueryString("url")); if(Surl == "undefined"){ Response.Write("<p style=\"font-size:9pt;margin:30px;padding:10px;text-align:center;background-color:#FFCCCC;border: 1px solid #999999;\">Asp代理 by Llinzzi</p>"); Response.Write("<div style=\"font-size:9pt;margin:30px;text-align:center;background-color:#FFFFCC;border: 1px solid #999999;\">"); Response.Write("<form action=\"#\" method=\"get\" >"); Response.Write("<p style=\"font-size:9pt;margin:5px;text-align:center;\">请输入网址</p>"); Response.Write("<p><input style=\"background-color: #FFCCCC;border: 1px solid #999999;\" type=\"text\" name=\"url\"></p>"); Response.Write("<p><input style=\"background-color: #FFCCCC;border: 1px solid #999999;\" type=\"submit\" value=\"GoWeb\"></p>"); Response.Write("</div>"); } else if(Surl == ""){ Response.Write("<p style=\"font-size:9pt;margin:30px;padding:10px;text-align:center;background-color:#FFCCCC;border: 1px solid #999999;\">Asp代理 by Llinzzi</p>"); Response.Write("<div style=\"font-size:9pt;margin:30px;text-align:center;background-color:#FFFFCC;border: 1px solid #999999;\">"); Response.Write("<p>地址为空,请格式输入 如 www.tom.com </p>"); Response.Write("</div>"); } else{ Surl = (Surl.substr(0,7) != "http://") ? "http://"+Surl : Surl; Response.Write(send_request(Surl)); } function send_request(url) { var codedtext; http_request = Server.CreateObject("Microsoft.XMLHTTP"); try{ http_request.Open("GET",url,false); http_request.Send(null); } catch(e) { Response.Write(e.description); } if (http_request.ReadyState == 4){ //自动判断编码开始 var charresult = http_request.ResponseText.match(/CharSet=(\S+)"/i); if (charresult != null){ var Cset = charresult[1]; }else{Cset = "utf-8"} //自动判断编码结束 codedtext = bytesToBSTR(http_request.Responsebody,Cset); }else{ codedtext = "Erro"; } //替换超连接 codedtext = codedtext.replace(/href="\/?/ig,"href=\""+url+"/"); codedtext = codedtext.replace(/(<a.*href=")/ig,"$1servergate.asp?url="); return(codedtext); } function bytesToBSTR(body,Cset){ var objstream; objstream = Server.CreateObject("Adodb.Stream"); objstream.Type = 1; objstream.Mode = 3; objstream.Open(); objstream.Write(body); objstream.Position = 0; objstream.Type = 2; objstream.Charset = Cset; bytesToBSTR = objstream.Readtext; objstream.Close; return(bytesToBSTR); } function SaveRemoteFile(LocalFileName,RemoteFileUrl){ var Retrieval,Ads; Retrieval = Server.CreateObject("Microsoft.XMLHTTP"); Retrieval.Open("GET",RemoteFileUrl,false); Retrieval.Send(null); if (Retrieval.ReadyState == 4){ Ads = Server.CreateObject("Adodb.Stream"); Ads.Type = 1; Ads.Open(); Ads.Write(Retrieval.Responsebody); Ads.SaveToFile(Server.MapPath(LocalFileName),2); Ads.Cancel; Ads.Close; } } //SaveRemoteFile("aa.gif","http://www.baidu.com/img/logo.gif"); %> [/code]