底层连接已关闭:使用HttpWebRequest意外关闭连接

时间:2017-01-10 16:45:46

标签: c# httpwebrequest httpwebresponse

我正在做一个应用程序来向服务器Web应用程序发出POST请求以插入到数据库中。但是我收到了这个错误:error message box

  

基础连接已关闭:连接意外关闭。

这是我的代码:

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();
                }

我尝试了很多方法,但它仍然没有用。对此有何看法?

1 个答案:

答案 0 :(得分:0)

[已解决]服务器中最长执行时间的问题。 如果有人像我一样得到了他的问题,请检查服务器而不是客户端的执行时间。