Click here to Skip to main content
15,892,965 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
C#
try
            {
                HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
                request.Method = apiCallMethod;
                request.ContentType = "application/json";
                request.Accept = "en_US";
                request.Headers.Add("X-Api-Version:" + xApiVersion);
                var temp = "";
                //request.Headers.Add("X-Api-Version:" + xApiVersion);
                //request.Headers.Add("auth:" + sessionID);
                //if (apiCallMethod == "POST")
                //{
                //    using (var streamWriter = new StreamWriter(request.GetRequestStream()))
                //    {
                //        streamWriter.Write(json);
                //        streamWriter.Flush();
                //        streamWriter.Close();
                //    }
                //}
                ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3;
                request.Timeout = 300;
                HttpWebResponse response = (HttpWebResponse)request.GetResponse();
                string result;


What I have tried:

Error on line
C#
HttpWebResponse response = (HttpWebResponse)request.GetResponse();

Error- the underlying connection was closed unexpected error occurred on a receive
Posted
Updated 18-Feb-18 22:58pm
v2

1 solution

When this happens, it means that the remote server has forcefully terminated the connection. Whenever I have seen this, it has usually been because the calling code was passing an incorrect URL that caused a hard failure at the server side, there was a configuration issue (such as not supplying valid auth tokens) or there was a problem with the remote server. Either way, the code you have shown here isn't going to help you because you are missing the URL. If you can guarantee that the URL is correct and that you have satisfied all the conditions on your side, you are going to have to work with the people at the other side of the calling chain to work out what's wrong at their end.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900