本文仅针AD下代理上网的情况:
代码如下:
1、定义变量:
定义变量#region定义变量
复制代码 代码如下:
privatestringstrFireWallIP
...{
get
...{
returnSystem.Configuration.ConfigurationSettings.AppSettings["strFireWallIP"];
}
}
privatestringstrFireWallPort
...{
get
...{
returnSystem.Configuration.ConfigurationSettings.AppSettings["strFireWallPort"];
}
}
privatestringstrUID
...{
get
...{
returnSystem.Configuration.ConfigurationSettings.AppSettings["strUID"];
}
}
privatestringstrPWD
...{
get
...{
returnSystem.Configuration.ConfigurationSettings.AppSettings["strPWD"];
}
}
privatestringstrDomain
...{
get
...{
returnSystem.Configuration.ConfigurationSettings.AppSettings["strDomain"];
}
}
#endregion
方法:
获取指定远程网页内容
复制代码 代码如下:
/**////<summary>
///获取指定远程网页内容
///</summary>
///<paramname="strUrl">所要查找的远程网页地址</param>
///<returns></returns>
//[WebMethod(Description="获取指定远程网页内容。")]
publicstringgetPageContent(stringstrUrl)
...{
stringstrResult="";
this.CurrentUrl=strUrl;
if(this.CurrentUrl.ToLower().StartsWith("http://")==false)
this.CurrentUrl="http://"+this.CurrentUrl;
try
...{
contentBytes=GetHtmlByte(CurrentUrl);
}
catch(Exceptionerr)
...{
strResult="请求错误:"+err.Message;
}
if(contentBytes==null)
...{
thrownewException("没有获得返回值");
}
strResult=getStringFromByteArray(contentBytes,Encoding.UTF8);
returnstrResult;
}
获取指定远程网页元素字节数组::
获取指定远程网页元素字节数组#region获取指定远程网页元素字节数组
复制代码 代码如下:
/**////<summary>
///获取指定远程网页元素字节数组
///</summary>
///<paramname="strUrl">所要查找的远程网页地址</param>
///<returns></returns>
privatebyte[]GetHtmlByte(stringstrUrl)
...{
stringstrPara=(strUrl.IndexOf("?")>=0?strUrl.Substring(strUrl.IndexOf("?")+1):"");
System.Text.Encodingencoding=newUTF8Encoding();
byte[]byte1=encoding.GetBytes(strPara);
byte[]byteReturn=newbyte[10000000];
if(strUrl.Trim().ToLower().StartsWith("http://")==false)
...{
strUrl="http://"+strUrl;
}
HttpWebRequestmyHttpWebRequest=(HttpWebRequest)WebRequest.Create(strUrl);
myHttpWebRequest.AllowAutoRedirect=true;
myHttpWebRequest.KeepAlive=true;
myHttpWebRequest.UserAgent="Mozilla/4.0(compatible;MSIE6.0;WindowsNT5.0;.NETCLR1.1.4322)";
System.Net.WebProxyproxy=newWebProxy(strFireWallIP+":"+strFireWallPort,true);
//proxy=(WebProxy)System.Net.GlobalProxySelection.Select;
System.Net.NetworkCredentialmyCredential=newNetworkCredential(strUID,strPWD,strDomain);
proxy.Credentials=myCredential;
myHttpWebRequest.Proxy=proxy;
HttpWebResponsemyHttpWebResponse=(HttpWebResponse)myHttpWebRequest.GetResponse();
byte[]bRead=newbyte[1024];
intlngCount=1;
inttotalLen=0;
StreamrecWeb=myHttpWebResponse.GetResponseStream();
lngCount=recWeb.Read(bRead,0,1024);
while(lngCount>0)
...{
Array.Copy(bRead,0,byteReturn,totalLen,lngCount);
totalLen+=lngCount;
lngCount=recWeb.Read(bRead,0,1024);
}
recWeb.Close();
byte[]byteGets=newbyte[totalLen];
Array.Copy(byteReturn,0,byteGets,0,totalLen);
byteReturn=null;
bRead=null;
returnbyteGets;
}
#endregion
转换指定字节数组为字符串::
转换指定字节数组为字符串#region转换指定字节数组为字符串
复制代码 代码如下:
/**////<summary>
///转换指定字节数组为字符串
///</summary>
///<paramname="ByteGet">字节数组Byte[]</param>
///<paramname="myEncoding">编码方式</param>
///<returns></returns>
privatestaticstringgetStringFromByteArray(Byte[]ByteGet,EncodingmyEncoding)
...{
inti,lngCount;
StringBuilderaTemp=newStringBuilder(10000);
lngCount=ByteGet.Length;
for(i=0;i<lngCount;i+=10000)
...{
aTemp.Append(myEncoding.GetString(ByteGet,i,(lngCount>=i+10000?10000:lngCount-i)));
}
if(i<=lngCount)
...{
aTemp.Append(myEncoding.GetString(ByteGet,i,(lngCount-i)));
}
returnaTemp.ToString();
}
#endregion
借用这个,写了个抽取中国天气网预报的服务!很爽!