Common Errors of HttpWebRequest can be avoided by using TcpClient

Source: Internet
Author: User
Tags html header

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 "";
}
}

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.