您的位置:首页 > 其它

关于web自动化操作的分析和基类的实现

2012-04-18 17:59 218 查看
关于HTTP协议
http协议是一种无状态协议。这是首先要明确的,客户端(client)发送一个请求,服务端(server)收到之后,根据请求的URL和HTTP头信息,给出相应的答案。505,404,400等,一般正确的200,服务器除了IP和UserAgent等常用信息之外,服务器无法知道具体的标示,也就是说服务器无法知道这个请求来自哪个客户端,OK!
那么就引入了Cookie的概念,服务器一般用cookie去标示客户端,可见cookie对于现在web系统的重要性,如果没有cookie现在的web啥不是。
也就是说Cookie的web交互核心之一
要实现web自动化操作,就要控制Cookie以及http头等信息,现在假设一个场景,
QQ邮箱登陆:
1,登陆QQ邮箱的主地址(http://mail.qq.com)



请求头如上




响应内容,跳转到登陆页(因为没有登陆后的cookie标示)
2,会经过几个跳转步骤之后跳转到HTTPS登陆(https://mail.qq.com/cgi-bin/loginpage?&res=local)




3,输入账号登陆
输入密码后会跳转到

使用get方式提交表单,如果登陆成功会写Cookie




4,登陆成功之后我们再次进入通过mail.qq.com域名进入,也会跳转到登陆页,但是由于请求头中的cookie已经包含登陆标示,所以会直接跳转到邮箱url




重上述过程可以看出用户验证和识别都是依赖于Cookie,那么我们就需要一个能够控制Cookie,能够定制HTTP头,能发起HTTP请求的功能模块,也许有人会问为什么不用webClient或webBrowser,这个要重2方面说明,一个是性能webBrowser会加载页面所有的东西,而且共享IE浏览器的信息,我们是需要独立的,因为有可能会一个程序同时登陆多个用户,WebClient在提交Post数据一个获取HTTP头方面比较弱,所有就只能是webReques和自定义Cookie组合
Cookie自定义管理实现

publicclassCookieManager
{/**
*ParseaCookie:headerintoindividualtokensaccordingtoRFC2109.
*/
privateclassCookieTokenizer
{
/**
*Upperboundonthenumberofcookietokenstoaccept.Thelimitis
*basedonthe4differentattributes(4.3.4)andthe20cookieminimum
*(6.3)giveninRFC2109multipliedby2toaccomodatethe2tokensin
*eachname=valuepair("JSESSIONID=1234"is2tokens).
*/
privateconstintMAX_COOKIE_TOKENS=4*20*2;
/**
*Arrayofcookietokens.Evenindicescontainnametokenswhileodd
*indicescontainvaluetokens(ornull).
*/
publicstring[]tokens=newstring[MAX_COOKIE_TOKENS];
/**
*Numberofcookietokenscurrentlyinthetokens[]array.
*/
privateintnumTokens=0;
/**
*Parseaname=valuepairfromtheCookie:header.
*
*@paramcookiesTheCookie:headertoparse
*@parambeginIndexTheindexincookiestobeginparsingfrom,inclusive
*/
privateintparseNameValue(stringcookies,intbeginIndex)
{
intlength=cookies.Length;
intindex=beginIndex;
while(index<length)
{
switch(cookies[index])
{
case';':
case',':
//Foundendofnametokenwithoutvalue
tokens[numTokens]=cookies.Substring(beginIndex,index-beginIndex).Trim();
if(tokens[numTokens].Length>0)
{
numTokens++;
tokens[numTokens]=null;
numTokens++;
}
returnindex+1;
case'=':
//Foundendofnametokenwithvalue
tokens[numTokens]=cookies.Substring(beginIndex,index-beginIndex).Trim();
numTokens++;
returnparseValue(cookies,index+1);
case'"':
//Skippastquotedspan
doindex++;while(cookies[index]!='"');
break;
}
index++;
}
if(index>beginIndex)
{
//Foundendofnametokenwithoutvalue
tokens[numTokens]=cookies.Substring(beginIndex,index-beginIndex).Trim();
if(tokens[numTokens].Length>0)
{
numTokens++;
tokens[numTokens]=null;
numTokens++;
}
}
returnindex;
}
/**
*Parsethename=valuetokensfromaCookie:header.
*
*@paramcookiesTheCookie:headertoparse
*/
publicinttokenize(stringcookies)
{
numTokens=0;
if(cookies!=null)
{
try
{
//Advancethroughcookies,parsingname=valuepairs
intlength=cookies.Length;
intindex=0;
while(index<length)
index=parseNameValue(cookies,index);
}
catch(Exceptione)
{
//Filledupthetokens[]array
}
//catch(IndexOutOfBoundsExceptione)
//{
////Walkedofftheendofthecookiesheader
//}
}
returnnumTokens;
}
/**
*ReturnthenumberofcookietokensparsedfromtheCookie:header.
*/
publicintgetNumTokens()
{
returnnumTokens;
}
/**
*ReturnsagivencookietokenfromtheCookie:header.
*
*@paramindexTheindexofthecookietokentoreturn
*/
publicStringtokenAt(intindex)
{
returntokens[index];
}
/**
*Parsethevaluetokenfromaname=valuepair.
*
*@paramcookiesTheCookie:headertoparse
*@parambeginIndexTheindexincookiestobeginparsingfrom,inclusive
*/
privateintparseValue(Stringcookies,intbeginIndex)
{
intlength=cookies.Length;
intindex=beginIndex;
while(index<length)
{
switch(cookies[index])
{
case';':
case',':
//Foundendofvaluetoken
tokens[numTokens]=cookies.Substring(beginIndex,index-beginIndex).Trim();
numTokens++;
returnindex+1;
case'"':
//Skippastquotedspan
doindex++;while(cookies[index]!='"');
break;
}
index++;
}
//Foundendofvaluetoken
tokens[numTokens]=cookies.Substring(beginIndex,index-beginIndex).Trim();
numTokens++;
returnindex;
}
}
staticRegexregExpires=newRegex(@"expires\=[\s]*?[\w]+.+?(?=(,|;|[\w-]+\=|$))",RegexOptions.IgnoreCase);
staticRegexregHttpOnly=newRegex(@"httponly([\s]*?|,)",RegexOptions.IgnoreCase);
staticCookieTokenizerct=newCookieTokenizer();
staticstring[]systemKey=newstring[]{
"expires","domain","path","max-age","version"
};
staticpublicList<Cookie>ParseCookieHeader(stringcookieHeader,stringdefaultDomain)
{
List<Cookie>cList=newList<Cookie>();
varmasExpires=regExpires.Matches(cookieHeader);
foreach(MatchmaExpiresinmasExpires)
{
stringdateText=maExpires.Value.Trim().Substring(8,maExpires.Value.Trim().Length-8);
if(dateText.IndexOf(",")==dateText.Length-1||dateText.IndexOf(";")==dateText.Length-1)
{
dateText=dateText.Substring(0,dateText.Length-1);
}
if(dateText.IndexOf(",")==3)
{
dateText=dateText.Substring(3,dateText.Length-3);
}
DateTimedate=DateTime.Parse(dateText);
cookieHeader=cookieHeader.Replace(maExpires.Value,"expires="+date.Ticks.ToString()+";");
}
cookieHeader=regHttpOnly.Replace(cookieHeader,"");
intcount=ct.tokenize(cookieHeader);
stringkey="";
Cookiecookie=null;
for(inti=0;i<count;i++)
{
if(i%2==0)
{
key=ct.tokens[i];
}
else
{
if(key!="")
{
if(!systemKey.Contains(key.ToLower()))
{
cookie=newCookie();
cookie.Name=key;
cookie.Value=ct.tokens[i];
cookie.Path="/";
cookie.Expires=DateTime.Now.AddDays(1);
cookie.Domain=defaultDomain;
if(cList.Count(p=>p.Name.ToLower().Trim()==key.ToLower().Trim())>0)
{
cList.Remove(cList.Where(p=>p.Name.ToLower().Trim()==key.ToLower().Trim()).Take(1).Single());
}
cList.Add(cookie);
}
else
{
if(cookie!=null)
{
if(key.ToLower()==systemKey[0])
{
try
{
cookie.Expires=cookie.Expires.AddMilliseconds(double.Parse(ct.tokens[i])/10000);
}
catch{cookie.Expires=DateTime.Now.AddDays(1);}
}
elseif(key.ToLower()==systemKey[1])
{
cookie.Domain=ct.tokens[i];
}
elseif(key.ToLower()==systemKey[2])
{
cookie.Path=ct.tokens[i];
}
elseif(key.ToLower()==systemKey[3])
{
try
{
cookie.Expires=cookie.Expires.AddSeconds(double.Parse(ct.tokens[i]));
}
catch{cookie.Expires=DateTime.Now.AddDays(1);}
}
}
}
}
}
}
returncList;
}
List<Cookie>cookieList=newList<Cookie>();
publicvoidSetCookie(CookieCollectioncookies)
{
foreach(Cookiecookieincookies)
{
if(cookieList.Count(p=>p.Name==cookie.Name&&p.Domain.ToLower()==cookie.Domain.ToLower())>0)
{
vartc=cookieList.Where(p=>p.Name==cookie.Name&&p.Domain.ToLower()==cookie.Domain.ToLower()).Take(1).Single();
cookieList.Remove(tc);
}
cookieList.Add(cookie);
}
}
publicvoidSetCookie(List<Cookie>cookies)
{
CookieCollectioncc=newCookieCollection();
foreach(Cookiecookieincookies)
{
cc.Add(cookie);
}
SetCookie(cc);
}
publicvoidSetCookie(stringcookieHeader,stringdefaultDomain)
{
SetCookie(ParseCookieHeader(cookieHeader,defaultDomain));
}
publicvoidSetCookie(Cookieck)
{
CookieCollectioncc=newCookieCollection();
cc.Add(ck);
SetCookie(cc);
}
publicstringGetCookieHeader(stringhost)
{
varwhe=GetCookies(host);
returnGetCookieString(whe);
}
staticRegexregDomain=newRegex(@"[\w]+\.(org\.cn|net\.cn|com\.cn|com|net|org|gov|cc|biz|info|cn|hk)+$");
publicCookieCollectionGetCookies(stringserverHost)
{
List<string>domainList=newList<string>();
stringdomain=regDomain.Match(serverHost).Value;
stringhost=serverHost.ToLower().Replace("www.","");
host=host.Replace(domain,"domain");
string[]pars=host.Split('.');
if(pars.Length>1)
{
stringtmp="";
for(inti=pars.Length-1;i>-1;i--)
{
if(pars[i]=="domain")
continue;
tmp=pars[i]+"."+tmp;
domainList.Add(tmp+domain);
domainList.Add("."+tmp+domain);
}
}
domainList.Add(serverHost);
domainList.Add(domain);
domainList.Add("."+domain);
CookieCollectioncc=newCookieCollection();
varwhe=cookieList.Where(p=>domainList.Contains(p.Domain.ToLower()));
foreach(varcookieinwhe)
{
cc.Add(cookie);
}
returncc;
}
publicCookieCollectionConvert(List<Cookie>cks)
{
CookieCollectioncc=newCookieCollection();
foreach(Cookieitemincks)
{
cc.Add(item);
}
returncc;
}
publicList<Cookie>Convert(CookieCollectioncks)
{
List<Cookie>cc=newList<Cookie>();
foreach(Cookieitemincks)
{
cc.Add(item);
}
returncc;
}
privatestringGetCookieString(CookieCollectioncks)
{
StringBuilderstrCookie=newStringBuilder();
foreach(Cookiecookieincks)
{
strCookie.Append(cookie.Name);
strCookie.Append("=");
strCookie.Append(cookie.Value);
strCookie.Append(";");
}
if(strCookie.Length>3)
strCookie=strCookie.Remove(strCookie.Length-2,2);
returnstrCookie.ToString();
}
publicvoidSetAllCookieToDomain(stringdomain)
{
varatCookie=Convert(GetCookies(domain));
varneedCookie=cookieList.Where(p=>!atCookie.Contains(p)).ToArray();
for(inti=0;i<needCookie.Length;i++)
{
Cookieitem=needCookie[i];
cookieList.Add(newCookie()
{
Domain=domain,
Expired=item.Expired,
Expires=item.Expires,
Name=item.Name,
Path=item.Path,
Value=item.Value
});
}
}
publicvoidClear()
{
cookieList.Clear();
}
publicvoidRemoveCookie(stringname,stringdoamin=null)
{
varcks=newList<Cookie>();
vartemp=newCookie[cookieList.Count];
cookieList.CopyTo(temp);
cks=temp.ToList();
if(!string.IsNullOrEmpty(doamin))
{
cks=Convert(GetCookies(doamin));
}
foreach(Cookiecookieincks)
{
if(cookie.Name.Trim()==name.Trim())
{
cookieList.Remove(cookie);
}
}
}
publicvoidSetIECookie(stringhost=null)
{
varcks=cookieList;
if(!string.IsNullOrEmpty(host))
{
cks=Convert(GetCookies(host));
}
foreach(varcookieincookieList)
{
for(inti=0;i<5&&!WinAPI.InternetSetCookie("http://"+(cookie.Domain.StartsWith(".")?"www":"")+cookie.Domain,cookie.Name,cookie.Value+";expires="+cookie.Expires.ToGMTString());i++);
}
}
}

.csharpcode,.csharpcodepre
{
font-size:small;
color:black;
font-family:consolas,"CourierNew",courier,monospace;
background-color:#ffffff;
/*white-space:pre;*/
}
.csharpcodepre{margin:0em;}
.csharpcode.rem{color:#008000;}
.csharpcode.kwrd{color:#0000ff;}
.csharpcode.str{color:#006080;}
.csharpcode.op{color:#0000c0;}
.csharpcode.preproc{color:#cc6633;}
.csharpcode.asp{background-color:#ffff00;}
.csharpcode.html{color:#800000;}
.csharpcode.attr{color:#ff0000;}
.csharpcode.alt
{
background-color:#f4f4f4;
width:100%;
margin:0em;
}
.csharpcode.lnum{color:#606060;}

自定义web操作类

abstractpublicclassWebAction
{
/*类名:web操作基础支持类
*描述:提供web操作基础接口
*创建日期:2011-10-25
*版本:0.4
*作者:byrolends986
*/
/*
*版本更新记录
*0.1基本代码的构建与调试
*0.2修改主入口方法,实现多参数化定义2011-11-1
*0.3添加SetUseUnsafeHeaderParsing功能,修订lock逻辑,删除url编码逻辑(会导致部分服务器header解析问题)2011-12-2
*0.4新增代理控制逻辑,修改useUnsafeHeaderParsing参数,添加资源释放逻辑2011-12-12
*/
staticWebAction()
{
DefaultConnectionLimit=1000;
KeepAliveTime=10*1000;
KeepAliveInterval=300;
}
protectedCookieManager_cookieManager=newCookieManager();
protectedXQSoft.Common.LogLogObject{get{returnLogManager.Logs[LogName];}}
string_logName="";
virtualprotectedstringLogName{get{return_logName;}}
publicWebAction()
{
}
publicWebAction(stringlogName)
{
_logName=logName;
}
publicconststring_userAgent_FF="Mozilla/5.0(WindowsNT5.1;rv:11.0)Gecko/20100101Firefox/11.0";
publicconststring_userAgent_IE="Mozilla/4.0(compatible;MSIE7.0;WindowsNT5.1;Trident/4.0;.NETCLR2.0.50727;.NETCLR3.0.4506.2152;.NETCLR3.5.30729;.NET4.0C;.NET4.0E)";
publicconststring_defaultAccept="text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";
publicconststring_defaultAcceptLanguage="zh-cn,zh;q=0.5";
publicconststring_defaultAcceptCharset="GB2312,utf-8;q=0.7,*;q=0.7";
publicstaticintDefaultConnectionLimit{get;set;}
staticpublicintKeepAliveTime{get;set;}
staticpublicintKeepAliveInterval{get;set;}
publicboolEnableProxy{get;set;}
ProxyInfo_proxyInfo=null;
publicProxyInfoProxyInfo{get{return_proxyInfo;}protectedset{_proxyInfo=value;}}
publicstringKey{get{returnnewGuid().ToString();}}
staticobjectsslLock=newobject();
staticpublicboolCheckValidationResult(objectsender,X509Certificatecertificate,X509Chainchain,SslPolicyErrorserrors)
{
//if(senderisHttpWebRequest)
//{
//varrequest=senderasHttpWebRequest;
//if(request.ClientCertificates.Count>0)
//returnfalse;
//}
returntrue;//Alwaysaccept
}
virtualprotectedvoidChangeProxy()
{
_proxyInfo=ProxyManager.GetAvailableProxy(this.Key);
}
/*
新方法byrolends1986
*2011-10-27
1,支持自定义Content-Type
2,封装编码,自身维护表单键值转换
3,支持各种参数的自定义
4,实现自动编码
5,实现CA文件指定
*/
virtualprotectedTGetWebData<T>(stringurl,
stringcharset=null,
stringreferer=null,
PostInfopostInfo=null,
bool?useProxy=null,
NameValueCollectionheaders=null,
stringuserAgent=null,
Certificatescertificates=null,
Versionprotocol=null,
bool?allowAutoRedirect=false,
bool?keepAlive=null,
stringaccept=null,
stringacceptLanguage=null,
stringacceptCharset=null,
stringurlEncoding=null,
RequestCachePolicycachePolicy=null)
{
System.Net.ServicePointManager.DefaultConnectionLimit=DefaultConnectionLimit;
//System.Net.ServicePointManager.SetTcpKeepAlive(true,KeepAliveTime,KeepAliveInterval);
//SetUseUnsafeHeaderParsing(useUnsafeHeaderParsing);
varuri=newUri(url);
//url=EncodeUrl(url,urlEncoding);
varrequest=(HttpWebRequest)WebRequest.Create(url);
request.ServicePoint.Expect100Continue=false;
request.Proxy=null;
if(useProxy.HasValue)
{
if(useProxy.Value)
{
SetProxy(request);
}
}
else
{
if(EnableProxy)
{
SetProxy(request);
}
}
#regionsetdefault
request.KeepAlive=false;
request.AllowAutoRedirect=false;
request.UserAgent=_userAgent_FF;
request.Accept=_defaultAccept;
request.Headers.Add(HttpRequestHeader.AcceptLanguage,_defaultAcceptLanguage);
request.Headers.Add(HttpRequestHeader.AcceptCharset,_defaultAcceptCharset);
request.CachePolicy=newSystem.Net.Cache.RequestCachePolicy(System.Net.Cache.RequestCacheLevel.NoCacheNoStore);
request.Method="get";
#endregion
if(url.ToLower().IndexOf("https")==0)
{
if(certificates!=null)
{
X509CertificateCollectioncrts=null;
if(certificates.IsAuto)
{
crts=GetCertificates(uri);
}
else
{
crts=certificates.CertificateCollection;
}
if(crts==null)ThrowException(505,url);
request.ClientCertificates=crts;
}
request.ProtocolVersion=HttpVersion.Version10;
}
//request.Host=uri.Host;
if(allowAutoRedirect.HasValue)request.AllowAutoRedirect=allowAutoRedirect.Value;
//if(keepAlive.HasValue)request.KeepAlive=keepAlive.Value;//由于手动释放了资源,keepalive设置不再有效
if(!string.IsNullOrEmpty(userAgent))request.UserAgent=userAgent;
if(!string.IsNullOrEmpty(accept))request.Accept=accept;
if(!string.IsNullOrEmpty(acceptLanguage))
{
if(request.Headers[HttpRequestHeader.AcceptLanguage]==null)
request.Headers.Add(HttpRequestHeader.AcceptLanguage,acceptLanguage);
else
request.Headers[HttpRequestHeader.AcceptLanguage]=acceptLanguage;
}
if(!string.IsNullOrEmpty(acceptCharset))
{
if(request.Headers[HttpRequestHeader.AcceptCharset]==null)
request.Headers.Add(HttpRequestHeader.AcceptCharset,acceptCharset);
else
request.Headers[HttpRequestHeader.AcceptCharset]=acceptCharset;
}
if(!string.IsNullOrEmpty(referer))request.Referer=referer;
if(cachePolicy!=null)request.CachePolicy=cachePolicy;
if(protocol!=null)request.ProtocolVersion=protocol;
try
{
if(headers!=null)
foreach(varnvinheaders.AllKeys)
{
request.Headers.Add(nv,headers[nv]);
}
}
catch(Exceptionex)
{
DisposeRequest(request);
//requestheadererror502
ThrowException(502,ex.Message);
}
stringrequestCookie=_cookieManager.GetCookieHeader(uri.Host);
if(!String.IsNullOrEmpty(requestCookie))
{
request.Headers.Add(HttpRequestHeader.Cookie,requestCookie);
}
if(postInfo!=null)
{
request.Method="post";
byte[]byteArray=postInfo.GetPostData();
request.ContentType=postInfo.GetContentType();
request.ContentLength=byteArray.Length;
StreamdataStream=request.GetRequestStream();
dataStream.Write(byteArray,0,byteArray.Length);
dataStream.Close();
}
WebResponseresponse=null;
try
{
if(url.ToLower().IndexOf("https")==0)
{
lock(sslLock)
{
if(certificates!=null)
{
if(ServicePointManager.ServerCertificateValidationCallback!=null)
ServicePointManager.ServerCertificateValidationCallback-=CheckValidationResult;
}
else
{
if(ServicePointManager.ServerCertificateValidationCallback==null)
ServicePointManager.ServerCertificateValidationCallback+=CheckValidationResult;
}
response=request.GetResponse();
}
}
else
{
response=request.GetResponse();
}
}
catch(Exceptionex)
{
DisposeRequest(request);
DisposeResponse(response);
//getresponseerror503
ThrowException(503,ex.Message);
}
stringcookie=response.Headers.Get("Set-Cookie");
if(!String.IsNullOrEmpty(cookie))
{
_cookieManager.SetCookie(cookie,uri.Host);
}
varsm=response.GetResponseStream();
if(typeof(T)==typeof(string))
{
if(!String.IsNullOrEmpty(response.Headers["Content-Type"]))
{
string[]ct=response.Headers["Content-Type"].Split(';');
if(ct.Length>1)
{
charset=ct[1].Split('=')[1];//setserverresponseencoding
}
}
stringhtml=GetHtml(sm,charset);
Tresult=(T)(object)html;
DisposeRequest(request);
DisposeResponse(response);
returnresult;
}
elseif(typeof(Image)==typeof(T))
{
try
{
Imageoriginal=Image.FromStream(sm);
Tresult=(T)(object)original;
DisposeRequest(request);
DisposeResponse(response);
returnresult;
}
catch(Exceptionex)
{
//toimageerror504
DisposeRequest(request);
DisposeResponse(response);
ThrowException(504,ex.Message);
returndefault(T);
}
}
elseif(typeof(ResponseLocation)==typeof(T))
{
ResponseLocationrl=newResponseLocation()
{
Html=GetHtml(sm,charset),
Url=response.Headers["Location"]
};
Tresult=(T)(object)rl;
DisposeRequest(request);
DisposeResponse(response);
returnresult;
}
else
{
Tresult=(T)(object)GetData(sm);
DisposeRequest(request);
DisposeResponse(response);
returnresult;
}
}
privatevoidDisposeResponse(WebResponseresponse)
{
try
{
response.GetResponseStream().Close();
}
catch{}
try
{
response.Close();
}
catch{}
try
{
response=null;
}
catch{}
}
privatevoidDisposeRequest(HttpWebRequestrequest)
{
try
{
try
{
request.GetRequestStream().Close();
}
catch{}
try
{
request.Abort();
}
catch{}
try
{
request=null;
}
catch{}
}
catch{}
}
privatevoidSetProxy(HttpWebRequestrequest)
{
if(ProxyInfo==null)
ThrowException(533,"代理实例为空,请先实例化");
request.Proxy=newWebProxy(ProxyInfo.IPAddress.ToString(),ProxyInfo.Port);
}
publicstaticboolSetUseUnsafeHeaderParsing(boolboolVal)
{
try
{
Assemblyassem=Assembly.GetAssembly(typeof(System.Net.Configuration.SettingsSection));
if(assem==null)returnfalse;
TypeassemType=assem.GetType("System.Net.Configuration.SettingsSectionInternal");
if(assemType==null)returnfalse;
objectobj=assemType.InvokeMember("Section",BindingFlags.Static|BindingFlags.GetProperty|
BindingFlags.NonPublic,null,null,newobject[]{});
if(obj==null)returnfalse;
FieldInfofieldInfo=assemType.GetField("useUnsafeHeaderParsing",BindingFlags.NonPublic|BindingFlags.Instance);
if(fieldInfo==null)returnfalse;
fieldInfo.SetValue(obj,boolVal);
}
catch{}
returntrue;
}
privatestringEncodeUrl(stringurl,stringcode)
{
if(string.IsNullOrEmpty(code))
returnurl;
EncodingurlCode=Encoding.ASCII;
if(!String.IsNullOrEmpty(code))
{
urlCode=Encoding.GetEncoding(code);
}
intpIndex=url.IndexOf('?');
if(url.Length-1<=pIndex)
returnurl;
if(pIndex>1)
{
string[]its=url.Substring(pIndex+1).Split('&');
StringBuildernp=newStringBuilder();
foreach(varnvinits)
{
stringname="";
stringvalue="";
intcIndex=nv.IndexOf("=");
if(cIndex<0)
name=nv;
else
{
name=nv.Substring(0,cIndex);
if(nv.Length-1>cIndex)
value=nv.Substring(cIndex+1);
}
np.Append(UrlUnit.UrlEncode(name,urlCode));
np.Append("=");
np.Append(UrlUnit.UrlEncode(value,urlCode));
np.Append("&");
}
url=url.Substring(0,pIndex+1)+np.Remove(np.Length-1,1).ToString();
}
returnurl;
}
publicbyte[]GZipDecompress(byte[]gzip)
{
using(GZipStreamstream=newGZipStream(newMemoryStream(gzip),
CompressionMode.Decompress))
{
constintsize=4096;
byte[]buffer=newbyte[size];
using(MemoryStreammemory=newMemoryStream())
{
intcount=0;
do
{
count=stream.Read(buffer,0,size);
if(count>0)
{
memory.Write(buffer,0,count);
}
}
while(count>0);
returnmemory.ToArray();
}
}
}
publicbyte[]DeflateDecompress(byte[]deflate)
{
using(DeflateStreamstream=newDeflateStream(newMemoryStream(deflate),
CompressionMode.Decompress))
{
constintsize=4096;
byte[]buffer=newbyte[size];
using(MemoryStreammemory=newMemoryStream())
{
intcount=0;
do
{
count=stream.Read(buffer,0,size);
if(count>0)
{
memory.Write(buffer,0,count);
}
}
while(count>0);
returnmemory.ToArray();
}
}
}
privatebyte[]GetData(Streamsm)
{
byte[]realData=null;
byte[]buffer=newbyte[1024*8];
intdataLength=0;
do
{
dataLength=sm.Read(buffer,0,buffer.Length);
if(realData==null)
{
realData=newbyte[dataLength];
Array.Copy(buffer,realData,dataLength);
}
else
{
intoldLength=realData.Length;
Array.Resize<byte>(refrealData,realData.Length+dataLength);
Array.Copy(buffer,0,realData,oldLength,dataLength);
}
}
while(dataLength>0);
//return(T)(object)buffer.Take(dataLength).ToArray();
returnrealData;
}
privatestringGetHtml(Streamsm,stringcharset)
{
vardata=GetData(sm);
stringnewCharset=string.IsNullOrEmpty(charset)?"utf-8":charset;
try
{
stringr=Encoding.GetEncoding(newCharset).GetString(data);
if(string.IsNullOrEmpty(charset))
{
r=CheckEncoding(data,newCharset,r);
}
LogObject.WriteLine("==============================================\r\n");
LogObject.WriteLine(r);
LogObject.WriteLine("==============================================");
LogObject.WriteLine("******************************分割*************************");
returnr;
}
catch(Exceptionex)
{
//getresponseerror503
ThrowException(509,ex.Message);
return"";
}
}
protectedstaticRegexregCharset=newRegex("(?<=<meta.+?content\\=.+?charset\\=).+?(?=(\\\"|[\\s]))",RegexOptions.IgnoreCase);
protectedstaticRegexregCharset2=newRegex("(?<=<meta[\\s]+charset=[\\\"]{0,1})[a-z0-9]+(?=[\\\"]{0,1})",RegexOptions.IgnoreCase);
privatestringCheckEncoding(byte[]data,stringcurrentCharset,stringhtml)
{
stringpageCharset="";
if(regCharset.IsMatch(html))
{
pageCharset=regCharset.Match(html).Value.Trim().ToLower();
}
if(regCharset2.IsMatch(html))
{
pageCharset=regCharset2.Match(html).Value.Trim().ToLower();
}
if(pageCharset!=currentCharset.Trim().ToLower())
{
try
{
returnEncoding.GetEncoding(pageCharset).GetString(data);
}
catch{}
}
returnhtml;
}
virtualprotectedX509CertificateCollectionGetCertificates(Uriuri)
{
X509CertificateCollectioncerts=newX509CertificateCollection();
stringhost=uri.Host;
for(inti=0;i<8;i++)
{
for(intj=1;j<=2;j++)
{
X509Storestore=newX509Store((StoreName)(i+1),(StoreLocation)j);
store.Open(OpenFlags.MaxAllowed);
foreach(varcertinstore.Certificates)
{
Console.WriteLine(cert.Subject);
if(cert.Subject.ToLower().Contains(host.ToLower()))
{
certs.Add(cert);
}
}
}
}
returncerts;
}
virtualprotectedvoidThrowException(interrorCode,stringsourceText)
{
throwXQException.GetException(errorCode,sourceText);
}
protectedNameValueCollectionGetQueryParameter(stringurl)
{
NameValueCollectionpars=newNameValueCollection();
varparaString=url.Substring(url.IndexOf("?")+1,url.Length-url.IndexOf("?")-1).Split('&');
for(inti=0;i<paraString.Length;i++)
{
varnv=paraString[i].Split('=');
varname=nv[0];
varvalue=nv[1];
pars.Add(name,value);
}
returnpars;
}
}

.csharpcode,.csharpcodepre
{
font-size:small;
color:black;
font-family:consolas,"CourierNew",courier,monospace;
background-color:#ffffff;
/*white-space:pre;*/
}
.csharpcodepre{margin:0em;}
.csharpcode.rem{color:#008000;}
.csharpcode.kwrd{color:#0000ff;}
.csharpcode.str{color:#006080;}
.csharpcode.op{color:#0000c0;}
.csharpcode.preproc{color:#cc6633;}
.csharpcode.asp{background-color:#ffff00;}
.csharpcode.html{color:#800000;}
.csharpcode.attr{color:#ff0000;}
.csharpcode.alt
{
background-color:#f4f4f4;
width:100%;
margin:0em;
}
.csharpcode.lnum{color:#606060;}

QQ讨论组广告群发工具(已开发完成)索引
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: