失去连接时 ASIHTTPRequest 无错误
我正在下载文件(使用 apache tomcat 6.0.32)。当我断开连接(关闭 tomcat)时,有时 ASHITTPRequest 会生成错误,但有时(大多数时候完全正确...始终完全正确!仅当开始时没有连接时发生错误)它会像所有正确的那样结束工作。
所以有一个问题:为什么会发生这种情况以及如何观察连接是否正确丢失。
多谢!
更新:
尝试通过 TCPMon 发送请求,然后停止它(TCPMon)并得到相同的结果:ASIHTTPRequest 认为下载正确完成。
更新:
responseHeaders:
"Content-Disposition" = attachment;
"Content-Length" = 2277888;
"Content-Type" = "application/octet-stream";
Date = "Thu, 28 Apr 2011 12:35:32 GMT";
Server = "Apache-Coyote/1.1";
"Set-Cookie" = "JSESSIONID=98CAE6C0C4275B528D5E0F8651546AFE; Path=/ISED";
responseStatusMessage:
HTTP/1.1 200 OK
更新:
如果手动断开计算机(断开电缆)并得到以下信息:
有时 ASIHTTPRequest 会等待超时,下一个请求会出现连接错误。
有时在我断开连接时会发生错误。
有时,在断开连接时不会发生错误:下载进度丢失,并且(如果再次建立连接)文件从头开始下载。
如果关闭端口 ASIHTTPRequest 则认为下载成功。
我无法理解这个逻辑...
解决方案:
我通过比较标头中的内容长度和读取的总字节数来解决问题:
NSString *contentLength = [[self.request responseHeaders] valueForKey:@"Content-Length"];
NSString *downloadedBytesCount = [NSString stringWithFormat:@"%llu",[self.request totalBytesRead]];
if ([contentLength isEqualToString:downloadedBytesCount] == NO) {
// error maintain
}
这样我可以检查是否获得了所有数据,如果没有:则维持错误。 在其他情况下(例如手动断开连接),ASIHTTPRequest 以适当的方式工作并自行生成错误。
I am downloading file (with apache tomcat 6.0.32). When I make disconnect (shutdown tomcat) some times ASHITTPRequest generate error, but sometimes (most of times exactly...ALL TIME EXACTLY! Only if there is no connection at the beginning occurs error) it ends work like all correct.
So there is the question: why this happened and how can I watch if the connection lost properly.
Thanks a lot!
UPDATE:
Try to send request through TCPMon and then stop it (TCPMon) and get the same: ASIHTTPRequest think that the downloading correctly done.
UPDATE:
responseHeaders:
"Content-Disposition" = attachment;
"Content-Length" = 2277888;
"Content-Type" = "application/octet-stream";
Date = "Thu, 28 Apr 2011 12:35:32 GMT";
Server = "Apache-Coyote/1.1";
"Set-Cookie" = "JSESSIONID=98CAE6C0C4275B528D5E0F8651546AFE; Path=/ISED";
responseStatusMessage:
HTTP/1.1 200 OK
UPDATE:
If disconnect computer by hand (disconnect the cable) and get this:
Sometime ASIHTTPRequest waits till timeout, and next request get connecting error.
Sometime error occurs just in time I disconnect.
Sometime error does not occurs just in time I disconnect: download progress missing and (if make connection again) it file starts download from the beginning.
And if close port ASIHTTPRequest thinks that is successful download done.
I can't understand this logic...
SOLUTION:
I solve the problem by comparing Content-Length in header and total bytes read:
NSString *contentLength = [[self.request responseHeaders] valueForKey:@"Content-Length"];
NSString *downloadedBytesCount = [NSString stringWithFormat:@"%llu",[self.request totalBytesRead]];
if ([contentLength isEqualToString:downloadedBytesCount] == NO) {
// error maintain
}
So I can check if I got all data and if not: maintain error.
In other cases (like manually disconnect) ASIHTTPRequest work appropriate way and generates errors by self.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
为了检查连接状态,我使用带有 ASI 的可达性库。我相信 ASI 附带可达性,但不确定。
这不是我从某处获取的代码,但它工作完美。
我根据我的需要稍微修改了代码,它返回是/否。并且您应该在某个时候删除观察者。
谢谢。
for checking connection status i am using Reachability library with ASI. i believe Reachability comes with ASI not sure.
this is not mine code i took from somewhere but it is working flawless.
i have altered the code little bit regarding my needs it returns YES/NO. and you should remove observer at some point.
thanks.