Obtain the remote webpage content 2 (original downmoon)

Source: Internet
Author: User

In this article, only the active proxy is used to access the Internet:
The Code is as follows:
1. Define variables:

Define variable # region define variable
Copy codeThe Code is as follows:
Private string strFireWallIP
...{
Get
...{
Return System. Configuration. ConfigurationSettings. receivettings ["strFireWallIP"];
}
}
Private string strFireWallPort
...{
Get
...{
Return System. Configuration. ConfigurationSettings. receivettings ["strFireWallPort"];
}
}
Private string strUID
...{
Get
...{
Return System. Configuration. ConfigurationSettings. deleettings ["strUID"];
}
}
Private string strPWD
...{
Get
...{
Return System. Configuration. ConfigurationSettings. receivettings ["strPWD"];
}
}
Private string strDomain
...{
Get
...{
Return System. Configuration. ConfigurationSettings. deleettings ["strDomain"];
}
}
# Endregion



Method:
Obtains the content of a specified remote webpage.

 Copy codeThe Code is as follows:
/** // <Summary>
/// Obtain the content of the specified remote webpage
/// </Summary>
/// <Param name = "strUrl"> remote webpage address to be searched </param>
/// <Returns> </returns>
// [WebMethod (Description = "get the content of the specified remote webpage. ")]
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 = "request error:" + err. Message;
}
If (contentBytes = null)
...{
Throw new Exception ("No returned value ");
}
StrResult = getStringFromByteArray (contentBytes, Encoding. UTF8 );
Return strResult;
}




Obtain the element byte array of the specified remote webpage ::

Retrieve the array of specified remote webpage elements # region retrieve the array of specified remote webpage Elements
Copy codeThe Code is as follows:
/** // <Summary>
/// Obtain the element byte array of the specified remote webpage
/// </Summary>
/// <Param name = "strUrl"> remote webpage address to be searched </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




Convert the specified byte array to a string ::

Convert the specified byte array to a string # region convert the specified byte array to a string
Copy codeThe Code is as follows:
/** // <Summary>
/// Convert the specified byte array to a string
/// </Summary>
/// <Param name = "ByteGet"> Byte array [] </param>
/// <Param name = "myEncoding"> encoding method </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




I used this to write a service for extracting weather forecast from China! Great!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.