各位老大,急求:请求被中止: 连接被意外关闭

迷迷520 2011-06-12 05:54:25
请求被中止: 连接被意外关闭


在 System.Net.ConnectStream.Read(Byte[] buffer, Int32 offset, Int32 size)
在 System.IO.StreamReader.ReadBuffer()
在 System.IO.StreamReader.ReadLine()



如何解决
...全文
1616 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
sunny906 2011-06-13
  • 打赏
  • 举报
回复
用线程
迷迷520 2011-06-13
  • 打赏
  • 举报
回复
public void CloseHttp()
{
if (this.httpWebResponse != null)
{
this.httpWebResponse.Close();
}
this.httpWebResponse = null;
}
迷迷520 2011-06-13
  • 打赏
  • 举报
回复

public string GetResponseText(string url, string referer, Encoding encode)
{
if (encode == null)
{
encode = Encoding.GetEncoding("gb2312");
}

Stream responseStream = null;
StreamReader streamReader = null;

int num = this._tryTimes;
string html = String.Empty;
string tmpStr = String.Empty;
bool ishtml = false;

while (num-- > 0)
{
html = String.Empty;
tmpStr = String.Empty;
ishtml = false;

int millisecondsTimeout = MyUtils._rnd.Next(0, this._delayTime);
Thread.Sleep(millisecondsTimeout);

HttpWebRequest httpWebRequest = null;
try
{
httpWebRequest = (HttpWebRequest)WebRequest.Create(new Uri(url));
httpWebRequest.UserAgent = this._reqUserAgent;
httpWebRequest.CookieContainer = this._cc;
httpWebRequest.ServicePoint.ConnectionLimit = _maxTry;
httpWebRequest.Referer = referer;
httpWebRequest.Accept = this._accept;
httpWebRequest.KeepAlive = true;
httpWebRequest.Method = "GET";
httpWebRequest.Timeout = 2000000;
httpWebRequest.UnsafeAuthenticatedConnectionSharing = true;

httpWebResponse = (HttpWebResponse)httpWebRequest.GetResponse();
responseStream = httpWebResponse.GetResponseStream();
streamReader = new StreamReader(responseStream, encode);
while ((tmpStr = streamReader.ReadLine()) != null)
{
html += tmpStr;
}
ishtml = true;
}
catch (Exception msg)
{
MyUtils.ErrLog("GetResponseText-1.3", msg.TargetSite.Name, msg.Message, "转换URL:\r\n" + url + "\r\n数据:\r\n" + msg.StackTrace);
}
finally
{
streamReader.Close();
responseStream.Close();
this.CloseHttp();
}

if (ishtml) { break; } else { continue; }
}

return html;
}
迷迷520 2011-06-12
  • 打赏
  • 举报
回复
好的,谢谢 谢谢。

读DataSet的时候会特别的卡。窗体会白一会 才反映过来
暖枫无敌 2011-06-12
  • 打赏
  • 举报
回复
WinForm中数据量大的话,你要显示的话,就通过获取数据集DataSet,然后通过分页进行显示。

httpWebRequest
httpWebResponse
这些当然也是需要关闭的,都放在finally块中处理吧。
迷迷520 2011-06-12
  • 打赏
  • 举报
回复
httpWebRequest
httpWebResponse

这俩需要关闭吗??
迷迷520 2011-06-12
  • 打赏
  • 举报
回复
楼上高手,再请教一个问题,
像如WINFORM数据比较大的操作,应该如何处理 才不会是窗体卡
迷迷520 2011-06-12
  • 打赏
  • 举报
回复
万分感谢,我试下,只是有点不明白,不同样都是关闭吗
暖枫无敌 2011-06-12
  • 打赏
  • 举报
回复
将流定义放在你的循环外面,然后在finally块内关闭,你还在循环的时候就关闭了,肯定出问题了。

public string GetResponseText(string url, string referer, Encoding encode)
{
Stream responseStream ;
StreamReader streamReader;
if (encode == null)
{
encode = Encoding.GetEncoding("gb2312");
}
int num = this._tryTimes;
while (num-- > 0)
{
string html = String.Empty;
string tmpStr = String.Empty;

int millisecondsTimeout = MyUtils._rnd.Next(0, this._delayTime);
Thread.Sleep(millisecondsTimeout);
HttpWebRequest httpWebRequest = null;
try
{
httpWebRequest = (HttpWebRequest)WebRequest.Create(new Uri(url));
httpWebRequest.UserAgent = this._reqUserAgent;
httpWebRequest.CookieContainer = this._cc;
httpWebRequest.ServicePoint.ConnectionLimit = _maxTry;
httpWebRequest.Referer = referer;
httpWebRequest.Accept = this._accept;
httpWebRequest.KeepAlive = true;
httpWebRequest.Method = "GET";
httpWebRequest.Timeout = 2000000;
httpWebRequest.UnsafeAuthenticatedConnectionSharing = true;

httpWebResponse = (HttpWebResponse)httpWebRequest.GetResponse();
responseStream = httpWebResponse.GetResponseStream();
streamReader = new StreamReader(responseStream, encode);
while ((tmpStr = streamReader.ReadLine()) != null)
{
html += tmpStr;
}
return html;
}
catch (Exception msg)
{
MyUtils.ErrLog("GetResponseText-1.1", msg.TargetSite.Name, msg.Message, "转换URL:\r\n" + url + "\r\n数据:\r\n" + msg.StackTrace);

continue;
}
finally
{
streamReader.Close();
responseStream.Close();
}
}
return string.Empty;
}

迷迷520 2011-06-12
  • 打赏
  • 举报
回复


public string GetResponseText(string url, string referer, Encoding encode)
{
if (encode == null)
{
encode = Encoding.GetEncoding("gb2312");
}
int num = this._tryTimes;
while (num-- > 0)
{
string html = String.Empty;
string tmpStr = String.Empty;

int millisecondsTimeout = MyUtils._rnd.Next(0, this._delayTime);
Thread.Sleep(millisecondsTimeout);
HttpWebRequest httpWebRequest = null;
try
{
httpWebRequest = (HttpWebRequest)WebRequest.Create(new Uri(url));
httpWebRequest.UserAgent = this._reqUserAgent;
httpWebRequest.CookieContainer = this._cc;
httpWebRequest.ServicePoint.ConnectionLimit = _maxTry;
httpWebRequest.Referer = referer;
httpWebRequest.Accept = this._accept;
httpWebRequest.KeepAlive = true;
httpWebRequest.Method = "GET";
httpWebRequest.Timeout = 2000000;
httpWebRequest.UnsafeAuthenticatedConnectionSharing = true;

httpWebResponse = (HttpWebResponse)httpWebRequest.GetResponse();
Stream responseStream = httpWebResponse.GetResponseStream();
StreamReader streamReader = new StreamReader(responseStream, encode);
while ((tmpStr = streamReader.ReadLine()) != null)
{
html += tmpStr;
}
streamReader.Close();
responseStream.Close();
return html;
}
catch (Exception msg)
{
MyUtils.ErrLog("GetResponseText-1.1", msg.TargetSite.Name, msg.Message, "转换URL:\r\n" + url + "\r\n数据:\r\n" + msg.StackTrace);

continue;
}
}
return string.Empty;
}

暖枫无敌 2011-06-12
  • 打赏
  • 举报
回复
贴出代码看看,应该是你在读取数据的时候,你已经将文件流关闭造成的

110,538

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧