Common mistakes of HttpWebRequest can be avoided by using TcpClient

  • 2020-05-27 04:44:19
  • OfStack

Sometimes there are errors with HttpWebRequest objects. There are three ways to summarize them:
1, System.Net.WebException: the server submitted a protocol conflict. Section=ResponseStatusLine
2. System. Net. WebException: the base connection has been closed: the connection was accidentally closed.
3. System.Net.ProtocolViolationException: content body with this predicate type cannot be sent.
Do it with TcpClient objects:
 
private string GetHTMLTCP(string URL) 
{ 
string strHTML = "";// Used to save the acquired HTML code  
TcpClient clientSocket = new TcpClient(); 
Uri URI = new Uri(URL); 
clientSocket.Connect(URI.Host, URI.Port); 
StringBuilder RequestHeaders = new StringBuilder();// Used to store HTML Protocol header  
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\r\n"); 
Encoding encoding = Encoding.Default; 
byte[] request = encoding.GetBytes(RequestHeaders.ToString()); 
clientSocket.Client.Send(request); 
// Gets the network flow to save  
Stream readStream = clientSocket.GetStream(); 
StreamReader sr = new StreamReader(readStream, Encoding.Default); 
strHTML = sr.ReadToEnd(); 
readStream.Close(); 
clientSocket.Close(); 
return strHTML; 
} 

Request the page using HttpWebRequest
 
/// 
///  To obtain html The 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 it is Gzip The method requires decompression  
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 articles: