我正在使用 HTTP Post 方法并发起一个同步请求。 [NSURLConnection 发送同步请求:..]
对于 HTTP POST 请求,默认超时发生在 75 秒,正如许多线程中所讨论的那样。
但在 75 秒的超时期间,针对使用所有相同参数提出的同一请求,我们会启动多个 Web 服务请求。
请告诉我们是什么原因导致发起多个请求?这是由于一般的 HTTP POST 还是由于同步请求?
@iOS 示例代码
[body appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[request setHTTPBody:body];
[request addValue:[NSString stringWithFormat:@"%d", body.length] forHTTPHeaderField: @"Content-Length"];
[[NSURLCache sharedURLCache] setDiskCapacity:0];
[[NSURLCache sharedURLCache] setMemoryCapacity:0];
NSURLResponse *response;
response = nil;
urlData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
if(urlData)
{
NSString *responseString = [[NSString alloc] initWithData:urlData encoding:NSASCIIStringEncoding];
[self parseStringInformation:responseString infoDict:informationDictionary];
//NSLog(@"%@",responseString);
}
最佳答案
如果没有服务器的请求-响应日志,则有几种可能性。
程序员错误:您是否已经经历过所有“gotchya”类型的情况?
您是否在“urlData = [NSURLConnection sendSynchronousRequest: ...”行之前放置了一条日志消息,以确保您的代码只调用一次?
您是否从主 GUI 线程中调用此函数,如果您这样做不受支持/不推荐,这意味着它可能会导致副作用,如您所描述的那样。 https://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/NSURLConnection_Class/Reference/Reference.html
您确定您的请求设置为 POST 并且具有正确的 header ,例如“Content-type:multipart/form-data,boundary=X”
Web 服务器响应:如果没有 Web 服务器日志(或您发布到的服务的代码),很难说...
- 也许您的服务器正在向客户端发送循环重定向。如果您不实现“connection:willSendRequest”,那么重定向可以遵循 ?X?一个请求的次数。 http://developer.apple.com/library/mac/#documentation/cocoa/conceptual/urlloadingsystem/Articles/RequestChanges.html
API 错误:您发现了一些导致不良副作用的特殊情况。也许苹果有错误跟踪器或开发者支持论坛?
- 如果是这种情况,您将不得不解决该错误,直到它被修复。我建议实现异步调用链。 “异步加载数据” https://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/NSURLConnection_Class/Reference/Reference.html
关于ios - HTTP Post - 超时 - 在超时间隔内发起多个请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13266284/