Ajit More

Ajit More

  • NA
  • 569
  • 324.9k

Error While Integrating API ?

Aug 7 2019 1:58 AM
Hi friends,
 
When I am trying to push xml to webapi with basic authorization. the following error occured.
 
"The underlying connection was closed: An unexpected error occurred on a send."
 
Code
  1. string URL = "www.abc.com";  
  2. string xml_RN ="Some Xml File" ;  
  3. //Create a HttpWebRequest object  
  4. HttpWebRequest req_RN = (HttpWebRequest)WebRequest.Create(URL);  
  5. byte[] AuthBytes = Encoding.ASCII.GetBytes(UserName + ":" + Password);  
  6. string sAuth = Convert.ToBase64String(AuthBytes);  
  7. req_RN.Headers.Add("Authorization""Basic" + sAuth);  
  8. //Set the Credentials property  
  9. NetworkCredential cred = new NetworkCredential(UserName, Password);  
  10. req_RN.Credentials = cred;  
  11. req_RN.PreAuthenticate = true;  
  12. //Security Protocol  
  13. ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls12 | SecurityProtocolType.Ssl3;  
  14. ServicePointManager.Expect100Continue = true;  
  15. //Convert xml string to a byte array  
  16. byte[] postDataBytes = Encoding.ASCII.GetBytes(xml_RN);  
  17. //Set the Method property  
  18. req_RN.Method = "POST";  
  19. //Set the ContentType property of the "HttpWebRequest"  
  20. req_RN.ContentType = "application/xml";  
  21. req_RN.Accept = "application/xml";  
  22. //Set the ContentLength property of the "HttpWebRequest"  
  23. req_RN.ContentLength = postDataBytes.Length;  
  24. Stream requestStream = req_RN.GetRequestStream();  
  25. requestStream.Write(postDataBytes, 0, postDataBytes.Length);  
  26. requestStream.Close();  

Answers (3)