首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > C# >

c#的HttpWebResponse 如何老是报The underlying connection was closed

2013-09-12 
c#的HttpWebResponse 怎么老是报The underlying connection was closed我是用循环来调用的.不是马上出现,

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这个对象,调用他的方法吧.避免多线程调用太频繁,服务器有链接时长限制

热点排行