我有一个 Android 应用程序使用 Indy 10 TIdHttpServer(Delphi 2006 附带)与 Delphi 2006 Web 服务应用程序进行通信。 Delphi 应用程序生成一个大的 XML 文件并为其提供服务。 XML 生成可能持续超过 5 分钟。
如果持续时间为GenerateXml()
超过约 5 分钟 (*),我检测到错误 10053TIdHTTPResponseInfo.WriteContent
如果在 Delphi IDE 中运行:
Socket Error # 10053 Software caused connection abort.
但是,在 android 端没有检测到任何内容,并且HttpGet
- 通话永远持续。
我的问题是:
1.) 为什么我会收到错误 10053 以及如何避免它?看起来android连接超时了,但是http.socket.timeout
设置为无限。
and
2.) 我能做些什么来检测客户端的此类错误(除了设置超时,该超时必须太大而无用)?我可以在 TIdHttpServer.OnException 中做一些事情吗?
这是我的代码。
Android - 下载函数,在 AsyncTask 内运行:
protected static HttpEntity downloadEntity(String url) throws IOException {
HttpClient client = new DefaultHttpClient();
//Check because of Error 10053: but timeout is null -> infinite
Log.d("TAG", "http.socket.timeout: " + client.getParams().getParameter("http.socket.timeout"));
HttpGet get = new HttpGet(url);
HttpResponse response;
try {
//in case of Error 10053 the following call seems to last forever (in PlainSocketImpl.read)
response = client.execute(get);
} catch (ClientProtocolException e) {
//...
}
//...
return response.getEntity();
}
TIdHttpServer.OnCommandGet的Delphi实现:
procedure ServeXmlDoc(XmlDoc: IXMLDocument; ResponseInfo: TIdHTTPResponseInfo);
var
TempStream: TMemoryStream;
begin
ResponseInfo.ContentType := 'text/xml';
TempStream := TMemoryStream.Create;
XMLDoc.SaveToStream(TempStream);
ResponseInfo.FreeContentStream := True;
ResponseInfo.ContentStream := TempStream;
end;
procedure TMyService.HTTPServerCommandGet(AContext: TIdContext; RequestInfo: TIdHTTPRequestInfo;
ResponseInfo: TIdHTTPResponseInfo);
begin
Coinitialize(nil);
try
//...
ServeXmlDoc(GenerateXml(), ResponseInfo);
finally
CoUninitialize;
end;
end;
Edit:(*) 我做了进一步的测试,即使整个过程持续时间不到 2 分钟,也遇到了错误。