Rajveer singh

Rajveer singh

  • 799
  • 1k
  • 250.9k

The underlying connection was closed....

May 30 2018 3:53 AM
Hi all,
               Error :
The underlying connection was closed a connection that was expected to be kept alive was closed by the server 
 
 I'm using window form application and use API with Basic authentication, if I continue sending a 4-5 request to the server I again send request after 10-15 seconds then raised the issue. My Code here.
 
 
HttpWebRequest req = (HttpWebRequest)(HttpWebRequest.Create(URL));
req.Method = "POST";
req.ProtocolVersion = HttpVersion.Version10;
req.ContentType = "application/xml";
req.Timeout = ResponseTimeOut;
req.KeepAlive = false;
req.Headers.Set(HttpRequestHeader.Authorization, "Basic dshgfhjhgdshjsdhh==");
d1 = DateTime.Now;
string content = authXMLString;
req.ContentLength = content.Length;
Stream wri = req.GetRequestStream();
byte[] array = Encoding.UTF8.GetBytes(content);
wri.Write(array, 0, array.Length);
wri.Flush();
wri.Close();
HttpWebResponse HttpWResp = (HttpWebResponse)req.GetResponse();
int resCode = (int)HttpWResp.StatusCode;
StreamReader reader = new StreamReader(HttpWResp.GetResponseStream(), System.Text.Encoding.UTF8);
string resultData = reader.ReadToEnd();
d2 = DateTime.Now;
decodedresult = resultData;
decodedresult = decodedresult.Replace("<", "<");
decodedresult = decodedresult.Replace(">", ">");
 

Answers (4)