Sometimes an error occurs when you use the HttpWebRequest object. There are three types of errors:
1. System. Net. WebException: the server submitted a protocol conflict. Section = ResponseStatusLine
2. System. Net. WebException: the basic connection is closed. The connection is accidentally closed.
3. System. Net. ProtocolViolationException: the body of the content with this predicate type cannot be sent.
Use the TcpClient object.:
Copy codeThe Code is as follows:
Private string GetHTMLTCP (string URL)
{
String strHTML = ""; // used to save the obtained HTML code
TcpClient clientSocket = new TcpClient ();
Uri URI = new Uri (URL );
ClientSocket. Connect (URI. Host, URI. Port );
StringBuilder RequestHeaders = new StringBuilder (); // used to save HTML header information
RequestHeaders. AppendFormat ("{0} {1} HTTP/1.1 \ r \ n", "GET", URI. PathAndQuery );
RequestHeaders. AppendFormat ("Connection: close \ r \ n ");
RequestHeaders. AppendFormat ("Host: {0} \ r \ n", URI. Host );
RequestHeaders. AppendFormat ("Accept: */* \ r \ n ");
RequestHeaders. AppendFormat ("Accept-Language: zh-cn \ r \ n ");
RequestHeaders. appendFormat ("User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; SV1 ;. net clr 1.1.4322 ;. net clr 2.0.50727) \ r \ n ");
Encoding encoding = Encoding. Default;
Byte [] request = encoding. GetBytes (RequestHeaders. ToString ());
ClientSocket. Client. Send (request );
// Obtain the network stream to save
Stream readStream = clientSocket. GetStream ();
StreamReader sr = new StreamReader (readStream, Encoding. Default );
StrHTML = sr. ReadToEnd ();
ReadStream. Close ();
ClientSocket. Close ();
Return strHTML;
}
HttpWebRequest request page
Copy codeThe Code is as follows:
///
/// Obtain the html source code
///
///
///
///
Static string GetHTML (string url, string param)
{
Try
{
Uri uri = new Uri (url );
HttpWebRequest myReq = (HttpWebRequest) WebRequest. Create (uri );
MyReq. Headers. Add ("Accept-Encoding", "gzip, deflate"); // sdch
Byte [] byData = Encoding. Default. GetBytes (param );
MyReq. Method = "post ";
MyReq. ContentLength = byData. Length;
Stream reqStrem = myReq. GetRequestStream ();
ReqStrem. Write (byData, 0, byData. Length );
ReqStrem. Close ();
HttpWebResponse result = (HttpWebResponse) myReq. GetResponse ();
Stream recStream = result. GetResponseStream ();
// If the Gzip method is used, decompress the package.
RecStream = new GZipStream (recStream, CompressionMode. Decompress );
StreamReader redStream = new StreamReader (recStream, System. Text. Encoding. Default );
String strHTML = redStream. ReadToEnd ();
RedStream. Close ();
RecStream. Close ();
Result. Close ();
Return strHTML;
}
Catch (Exception)
{
Return "";
}
}