asp.net下擷取遠程網頁的內容之二(downmoon原創)

來源:互聯網
上載者:User

本文僅針AD下代理上網的情況:
代碼如下:
1、定義變數:
定義變數#region  定義變數 複製代碼 代碼如下:private string strFireWallIP
...{
get
...{
return System.Configuration.ConfigurationSettings.AppSettings["strFireWallIP"];
}
}
private string strFireWallPort
...{
get
...{
return System.Configuration.ConfigurationSettings.AppSettings["strFireWallPort"];
}
}
private string strUID
...{
get
...{
return System.Configuration.ConfigurationSettings.AppSettings["strUID"];
}
}
private string strPWD
...{
get
...{
return System.Configuration.ConfigurationSettings.AppSettings["strPWD"];
}
}
private string strDomain
...{
get
...{
return System.Configuration.ConfigurationSettings.AppSettings["strDomain"];
}
}
#endregion

方法:
擷取指定遠程網頁內容 複製代碼 代碼如下: /**//// <summary>
/// 擷取指定遠程網頁內容
/// </summary>
/// <param name="strUrl">所要尋找的遠程網頁地址</param>
/// <returns></returns>
//[WebMethod(Description = "擷取指定遠程網頁內容。")]
public string getPageContent(string strUrl)
...{
string strResult = "";
this.CurrentUrl = strUrl;
if(this.CurrentUrl.ToLower().StartsWith("http://")==false)
this.CurrentUrl = "http://"+this.CurrentUrl;
try
...{
contentBytes = GetHtmlByte(CurrentUrl);
}
catch(Exception err)
...{
strResult = "請求錯誤:" + err.Message;
}
if(contentBytes==null)
...{
throw new Exception("沒有獲得傳回值");
}
strResult = getStringFromByteArray(contentBytes,Encoding.UTF8);
return strResult;
}

擷取指定遠程網頁元素位元組數組::
擷取指定遠程網頁元素位元組數組#region 擷取指定遠程網頁元素位元組數組 複製代碼 代碼如下: /**//// <summary>
/// 擷取指定遠程網頁元素位元組數組
/// </summary>
/// <param name="strUrl">所要尋找的遠程網頁地址</param>
/// <returns></returns>
private byte[] GetHtmlByte(string strUrl)
...{
string strPara=(strUrl.IndexOf("?")>=0?strUrl.Substring(strUrl.IndexOf("?")+1):"");
System.Text.Encoding encoding = new UTF8Encoding();
byte[] byte1 = encoding.GetBytes(strPara);
byte[] byteReturn = new byte[10000000];
if(strUrl.Trim().ToLower().StartsWith("http://")==false)
...{
strUrl = "http://"+strUrl;
}
HttpWebRequest myHttpWebRequest = (HttpWebRequest)WebRequest.Create(strUrl);
myHttpWebRequest.AllowAutoRedirect = true;
myHttpWebRequest.KeepAlive = true;
myHttpWebRequest.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0; .NET CLR 1.1.4322)";
System.Net .WebProxy proxy = new WebProxy(strFireWallIP+":"+strFireWallPort,true);
//proxy=(WebProxy)System.Net.GlobalProxySelection.Select;
System.Net.NetworkCredential myCredential = new NetworkCredential(strUID,strPWD,strDomain);
proxy.Credentials =myCredential;
myHttpWebRequest.Proxy = proxy;
HttpWebResponse myHttpWebResponse = (HttpWebResponse)myHttpWebRequest.GetResponse();
byte[] bRead = new byte[1024];
int lngCount = 1;
int totalLen = 0;
Stream recWeb = 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 = new byte[totalLen];
Array.Copy(byteReturn,0,byteGets,0,totalLen);
byteReturn = null;
bRead = null;
return byteGets;
}
#endregion

轉換指定位元組數組為字串::
轉換指定位元組數組為字串#region 轉換指定位元組數組為字串 複製代碼 代碼如下: /**//// <summary>
/// 轉換指定位元組數組為字串
/// </summary>
/// <param name="ByteGet">位元組數組Byte[]</param>
/// <param name="myEncoding">編碼方式</param>
/// <returns></returns>
private static string getStringFromByteArray(Byte[] ByteGet,Encoding myEncoding)
...{
int i,lngCount;
StringBuilder aTemp = new StringBuilder(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)));
}
return aTemp.ToString();
}
#endregion

借用這個,寫了個抽取中國天氣網預報的服務!很爽!

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.