Rajveer singh

Rajveer singh

  • 804
  • 1k
  • 250.8k

connection was closed: An unexpected error on receive

Sep 6 2019 7:22 AM
Hi all,
 
C# HttpWebRequest The underlying connection was closed: An unexpected error occurred on a receive.
 
trying all the solutions but not get the solution.
 
OS: Windows 10 pro
 
server support this TLS version
 
1st method
  1. public void GET_request()  
  2. {   
  3. ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls12  
  4.  HttpWebRequest req = (HttpWebRequest)(HttpWebRequest.Create(url));  
  5.  req.Method = "POST";  
  6.  req.KeepAlive = false;  
  7.  req.ContentType = "application/xml";  
  8.  req.ProtocolVersion = HttpVersion.Version10;  
  9.  req.Headers.Set(HttpRequestHeader.Authorization, "Basic abcd15243");  
  10. string content = XDAuth.OuterXml;  
  11. req.ContentLength = content.Length;  
  12. Stream wri = null;  
  13. using (wri = req.GetRequestStream())  
  14. {  
  15. byte[] array = Encoding.UTF8.GetBytes(content);  
  16. wri.Write(array, 0, array.Length);  
  17. }  
  18. wri.Flush();  
  19. wri.Close();  
  20. string resultData = "";  
  21. using (HttpWebResponse HttpWResp = (HttpWebResponse)req.GetResponse())  
  22. {  
  23. int resCode = (int)HttpWResp.StatusCode;  
  24. using (Stream stream = HttpWResp.GetResponseStream())  
  25. {  
  26. using (StreamReader reader = new StreamReader(stream))  
  27. {  
  28. resultData = reader.ReadToEnd();  
  29. }  
  30. }  
  31. }  
  32. }   
2nd method
  1. protected override WebRequest GetWebRequest(Uri address)  
  2. {  
  3. WebRequest webRequest = base.GetWebRequest(address) as WebRequest;  
  4. if (webRequest == null)  
  5. {  
  6. return base.GetWebRequest(address);  
  7. }  
  8. webRequest.Method = "POST";  
  9. webRequest.ContentType = "Application/xml";  
  10. webRequest.Headers.Set(HttpRequestHeader.Authorization, "Basic abcd15243");  
  11. //webRequest.CookieContainer = Cookies;  
  12. return webRequest;  
  13. }
 

Answers (2)