日期:2014-05-17  浏览次数:21125 次

c#的HttpWebResponse 怎么老是报The underlying connection was closed
我是用循环来调用的.不是马上出现,出现的时间不定...奇怪了

string loginUrl = "http://192.168.1.101/DoServer.Class.php";
            Encoding encoding = Encoding.GetEncoding("UTF-8");

            HttpWebResponse response = HttpWebResponseUtility.CreatePostHttpResponse(loginUrl, parameters, null, null, encoding, null);
            //response.ti
            StreamReader sr = new StreamReader(response.GetResponseStream(), Encoding.UTF8);
            String retValue = sr.ReadToEnd();

            sr.Close();

            return retValue;


System.Net.WebException: The underlying connection was closed: A connection that was expected to be kept alive was closed by the server.
   at System.Net.HttpWebRequest.GetResponse()
   at WindowsEE.HttpWebResponseUtility.CreatePostHttpResponse(String url, IDictionary`2 parameters, Nullable`1 timeout, String userAgent, Encoding requestEncoding, CookieCollection cookies)
   at WindowsEE.MethodOption.common(IDictionary`2 parameters)
   at WindowsEE.Main.timer2_Tick(Object sender, EventArgs e)
   at System.Windows.Forms.Timer.OnTick(EventArgs e)
   at System.Windows.Forms.Timer.TimerNativeWindow.WndProc(Message& m)
   at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)

------解决方案--------------------
是不是多线程调用了同一个对象的同一个方法.
你在多线程里new这个对象,调用他的方法吧.避免多线程调用太频繁,服务器有链接时长限制