[搜片神器]BT种子下载超时很多的问题分析
继续接着第一篇写:使用C#实现DHT磁力搜索的BT种子后端管理程序+数据库设计(开源)[搜片神器]
谢谢园子朋友的支持,已经找到个VPS进行测试,国外的服务器: h31bt.org 大家可以给提点意见...
开源地址:https://github.com/h31h31/H31DHTMgr
程序下载:H31DHT下载
下载种子文件的时候失败很多,增加调试信息总是返回很多:Timeouts are not supported on this stream.
The remote server returned an error: (404) Not Found.
The operation has timed out.
附上之前的代码:
private int DownLoadFileToSaveFile(string strURL, string fileName) { Int32 ticktime1 = System.Environment.TickCount; try { Int32 ticktime2 = 0; byte[] buffer = new byte[4096]; WebRequest wr = WebRequest.Create(strURL); wr.ContentType = "application/x-bittorrent"; wr.Timeout = 300; WebResponse response = wr.GetResponse(); int readsize = 0; { bool gzip = response.Headers["Content-Encoding"] == "gzip"; Stream responseStream = gzip ? new GZipStream(response.GetResponseStream(), CompressionMode.Decompress) : response.GetResponseStream(); using (MemoryStream memoryStream = new MemoryStream()) { responseStream.ReadTimeout = 1000; int count = 0; do { count = responseStream.Read(buffer, 0, buffer.Length); memoryStream.Write(buffer, 0, count); readsize += count; Thread.Sleep(1); } while (count != 0); ticktime2 = System.Environment.TickCount; byte[] result = memoryStream.ToArray(); Thread.Sleep(10); using (BinaryWriter writer = new BinaryWriter(new FileStream(fileName, FileMode.Create))) { writer.Write(result); } } Int32 ticktime3 = System.Environment.TickCount; //H31Debug.PrintLn("下载成功" + strURL + ":" + readsize.ToString() + ":" + (ticktime2 - ticktime1).ToString() + "-" + (ticktime3 - ticktime2).ToString()); } return 1; } catch (Exception e) { Int32 ticktime3 = System.Environment.TickCount; //H31Debug.PrintLn("下载失败" + strURL + ":" + (ticktime3 - ticktime1).ToString()); return -2; } }
测试在国内服务器上情况很少时间,一放到国外服务器上就出现此问题,网上搜索资料最终显示是
参考1:http://stackoverflow.com/questions/9791423/httpwebresponse-readtimeout-timeouts-not-supported
经过代码分析,原来Stream responseStream里面使用的TIMEOUT参数设置.
Stream responseStream = gzip ? new GZipStream(response.GetResponseStream(), CompressionMode.Decompress) : response.GetResponseStream(); using (MemoryStream memoryStream = new MemoryStream()) { responseStream.ReadTimeout = 1000; int count = 0; do { count = responseStream.Read(buffer, 0, buffer.Length); memoryStream.Write(buffer, 0, count); readsize += count; Thread.Sleep(1); } while (count != 0); ticktime2 = System.Environment.TickCount; byte[] result = memoryStream.ToArray(); Thread.Sleep(10); using (BinaryWriter writer = new BinaryWriter(new FileStream(fileName, FileMode.Create))) { writer.Write(result); } }
然后为了防止程序界面卡住,错误的增加了Thread.Sleep(1);其它问题可能就出现在此地方,由于设置超时,然后数据流就被超时退出,从而被系统认为Stream没有注销导致异常,从而显示Timeouts are not supported on this stream.
修改后的代码为:
private int DownLoadFileToSaveFile(string strURL, string fileName,int timeout1) { Int32 ticktime1 = System.Environment.TickCount; try { Int32 ticktime2 = 0; byte[] buffer = new byte[4096]; WebRequest wr = WebRequest.Create(strURL); wr.ContentType = "application/x-bittorrent"; wr.Timeout = timeout1; WebResponse response = wr.GetResponse(); int readsize = 0; { bool gzip = response.Headers["Content-Encoding"] == "gzip"; Stream responseStream = gzip ? new GZipStream(response.GetResponseStream(), CompressionMode.Decompress) : response.GetResponseStream(); using (MemoryStream memoryStream = new MemoryStream()) { int count = 0; do { count = responseStream.Read(buffer, 0, buffer.Length); memoryStream.Write(buffer, 0, count); readsize += count; } while (count != 0); ticktime2 = System.Environment.TickCount; byte[] result = memoryStream.ToArray(); Thread.Sleep(10); using (BinaryWriter writer = new BinaryWriter(new FileStream(fileName, FileMode.Create))) { writer.Write(result); } } Int32 ticktime3 = System.Environment.TickCount; //H31Debug.PrintLn("下载成功" + strURL + ":" + readsize.ToString() + ":" + (ticktime2 - ticktime1).ToString() + "-" + (ticktime3 - ticktime2).ToString()); } return 1; } catch (WebException e) { Int32 ticktime3 = System.Environment.TickCount; if (e.Status == WebExceptionStatus.Timeout)//文件超时 { return -2; } else if (e.Status == WebExceptionStatus.ProtocolError)//文件不存在 { return -3; } else { H31Debug.PrintLn("下载失败" + strURL + ":" + (ticktime3 - ticktime1).ToString() + e.Status.ToString() + e.Message); return -4; } } }
测试程序后出现下载失败的HASH文件少了很多.
The remote server returned an error: (404) Not Found. 此问题是服务器没有此文件,可以采用if (e.Status == WebExceptionStatus.ProtocolError)来判断
The operation has timed out. 此问题是时间不够,可以增加 wr.Timeout = 300;这个时间的问题.
特此记录一下,希望大家多指教..大家可以从开源地址:https://github.com/h31h31/H31DHTMgr下载代码一起交流下..
大家觉得好的话,希望推荐支持下...
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 地球OL攻略 —— 某应届生求职总结
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 提示词工程——AI应用必不可少的技术
· .NET周刊【3月第1期 2025-03-02】