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" ;
- //Create a HttpWebRequest object
- 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);
- //Set the Credentials property
- NetworkCredential cred = new NetworkCredential(UserName, Password);
- req_RN.Credentials = cred;
- req_RN.PreAuthenticate = true;
- //Security Protocol
- ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls12 | SecurityProtocolType.Ssl3;
- ServicePointManager.Expect100Continue = true;
- //Convert xml string to a byte array
- byte[] postDataBytes = Encoding.ASCII.GetBytes(xml_RN);
- //Set the Method property
- req_RN.Method = "POST";
- //Set the ContentType property of the "HttpWebRequest"
- req_RN.ContentType = "application/xml";
- req_RN.Accept = "application/xml";
- //Set the ContentLength property of the "HttpWebRequest"
- req_RN.ContentLength = postDataBytes.Length;
- Stream requestStream = req_RN.GetRequestStream();
- requestStream.Write(postDataBytes, 0, postDataBytes.Length);
- requestStream.Close();

Aakash MauryaPosted Aug 7, 2019, 2:47 AM
Ajit MorePosted Aug 7, 2019, 2:40 AM
Aakash MauryaPosted Aug 7, 2019, 2:29 AM