Teemee Tang

Teemee Tang

  • NA
  • 111
  • 10.4k

Error the underlying connection was closed:an unexpected err

Dec 1 2017 5:22 AM
Hi guys,

Help please, I am experiencing an error message "The underlying connection was closed: An unexpected error occurred on a send." connecting to a cloud base RESTFUL API. It is working in staging and development server but not on production server.

My codes written in c#:

Declared in configuration file

<configuration>
<system.web>
<webServices>
<protocols>
<add name="HttpGet"/>
<add name="HttpPost"/>
</protocols>
</webServices>
<httpRuntime maxRequestLength="200000" executionTimeout="99999"/>
</system.web>
<system.net>
<settings>
<httpWebRequest useUnsafeHeaderParsing = "true"/>
</settings>
<defaultProxy useDefaultCredentials="true" >
</defaultProxy>
</system.net>**

public static string HttpGet(string url, string accessToken, string username)
{
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12 | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls | SecurityProtocolType.Ssl3;


HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
request.Method = "GET";
request.KeepAlive = false;
request.ContentLength = 0;
request.Timeout = 100000
request.Headers.Add("accessToken", accessToken);
request.Headers.Add("username", username);
HttpWebResponse response = (HttpWebResponse)request.GetResponse();

string result = null;

try
{
if (response.StatusCode.ToString().ToLower() == "ok")
{
string contentType = response.ContentType;
Stream content = response.GetResponseStream();
if (content != null)
{
StreamReader contentReader = new StreamReader(content);
result = contentReader.ReadToEnd();
}
}
}



Answers (2)