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
- string URL = "www.abc.com";
- string xml_RN ="Some Xml File" ;
-
- HttpWebRequest req_RN = (HttpWebRequest)WebRequest.Create(URL);
- byte[] AuthBytes = Encoding.ASCII.GetBytes(UserName + ":" + Password);
- string sAuth = Convert.ToBase64String(AuthBytes);
- req_RN.Headers.Add("Authorization", "Basic" + sAuth);
-
- NetworkCredential cred = new NetworkCredential(UserName, Password);
- req_RN.Credentials = cred;
- req_RN.PreAuthenticate = true;
-
- ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls12 | SecurityProtocolType.Ssl3;
- ServicePointManager.Expect100Continue = true;
-
- byte[] postDataBytes = Encoding.ASCII.GetBytes(xml_RN);
-
- req_RN.Method = "POST";
-
- req_RN.ContentType = "application/xml";
- req_RN.Accept = "application/xml";
-
- req_RN.ContentLength = postDataBytes.Length;
- Stream requestStream = req_RN.GetRequestStream();
- requestStream.Write(postDataBytes, 0, postDataBytes.Length);
- requestStream.Close();