Rajveer singh

Rajveer singh

  • 763
  • 1k
  • 240.8k

An unexpected error occurred on a send

Mar 19 2019 12:17 AM
THE UNDERLYING CONNECTION WAS CLOSED: AN UNEXPECTED ERROR OCCURRED ON A SEND
 
 
.net framework 4.5
Server TLS: TLS1.2
 
My code: Please guide me  how to resolve the issue.
 
HttpWebRequest req = (HttpWebRequest)(HttpWebRequest.Create(url));
req.Method = "POST";
req.KeepAlive = false;
req.ProtocolVersion = HttpVersion.Version10;
req.ContentType = "application/xml ";
req.Headers.Set(HttpRequestHeader.Authorization, "Basic API KEY");
ServicePointManager.SecurityProtocol =SecurityProtocolType.Tls12;
req.Timeout = 30000;
req.ReadWriteTimeout = 30000;
XmlDocument XDAuth = new XmlDocument();
XmlNode docNode = XDAuth.CreateXmlDeclaration("1.0", "UTF-8", "yes");
XDAuth.AppendChild(docNode);
XmlNode Root = XDAuth.AppendChild(XDAuth.CreateElement("xml"));
XmlNode otpnode = Root.AppendChild(XDAuth.CreateElement("Name"));
otpnode.InnerText = device;
otpnode = Root.AppendChild(XDAuth.CreateElement("ROOM"));
otpnode.InnerText = "PSL";
string content = XDAuth.OuterXml;
req.ContentLength = content.Length;
Stream wri = null;
using (wri = req.GetRequestStream())
{
byte[] array = Encoding.UTF8.GetBytes(content);
wri.Write(array, 0, array.Length);
}
wri.Flush();
wri.Close();
string resultData = "";
using (HttpWebResponse HttpWResp = (HttpWebResponse)req.GetResponse())
{
int resCode = (int)HttpWResp.StatusCode;
using (Stream stream = HttpWResp.GetResponseStream())
{
using (StreamReader reader = new StreamReader(stream))
{
resultData = reader.ReadToEnd();
resultData = resultData.Trim();
}
}
}
string decodedresult = resultData;
 
 

Answers (1)