0

Im doing an application to make a POST request to server web application to insert into database. But i got this error: error message box

The underlying connection was closed: The connection was closed unexpectedly.

Here's my code :

foreach (var it in dict)
                {
                      var httpWebRequest =   (HttpWebRequest)WebRequest.Create("http://mytestwebsitet.com/test.php");
                    httpWebRequest.ContentType = "application/json";
                    httpWebRequest.Method = "POST";
                    httpWebRequest.KeepAlive = false;
                    System.Net.ServicePointManager.Expect100Continue = false;
                    httpWebRequest.Timeout = 1000000;
                    httpWebRequest.ReadWriteTimeout = 1000000;
                    httpWebRequest.ProtocolVersion = HttpVersion.Version10;
                    //ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
                    using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream()))
                    {
                        streamWriter.Write(it);
                        streamWriter.Flush();
                        streamWriter.Close();
                    }

                    var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();
                    using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
                    {
                        var result = streamReader.ReadToEnd();
                    }
                    dataGridView1.Rows[dataGridView1.Rows.Count - co].DefaultCellStyle.BackColor = Color.Red;
                    co++;

                    httpResponse.Close();
                }

I've tried so many ways but it's still not working. Any idea for this ?

lamtacvu
  • 675
  • 1
  • 7
  • 14
  • In my experience, this error happened when the request made the service on the other end crashed (happened a lot when I was calling a service written in C/C++). Have you ruled out an issue on the service? – Martin Costello Jan 10 '17 at 17:17
  • Ofcourse i handled my code in webserver and in my service . i think the problem here is the request timeout but i dont know how to handle it – lamtacvu Jan 10 '17 at 17:43
  • in the webservice i set the timeout for request is 300s but in my desktop application it didnttt reach to it.so i think it's not an issue in webservice – lamtacvu Jan 10 '17 at 17:54

1 Answers1

0

[Solved] It's problem in maximum execution time in server. If someone got his issue like me, check the execution time in server not client.

lamtacvu
  • 675
  • 1
  • 7
  • 14
  • How do you check/set this? I'm using `WebClient.UploadDataTaskAsync` and it is failing right at 60 seconds. I've set the Request.Timeout to 120 seconds but it isn't listening to that. – Terry Mar 23 '19 at 14:26
  • @Terry maybe it helps :[this link](https://stackoverflow.com/questions/10649843/how-to-increase-executiontimeout-for-a-long-running-query) or [this link](https://stackoverflow.com/questions/2414441/how-to-increase-request-timeout-in-iis). you should check the execution time in your server's code. good luck. – lamtacvu Mar 24 '19 at 17:13