【发布时间】:2013-10-05 20:42:13
【问题描述】:
我正在设置一个包含数百个位置点的文档的异步发布请求。我想将此文档保存在我的 couchdb 中。
对于小型文档,请求响应在很短的时间内返回,存储文档。当文档增长一点(仍然
我是否缺少某些设置或编码错误?
- (void)post:(NSData*) requestBody {
startTime = [NSDate date];
NSURL* serverURL = [NSURL URLWithString: @"https://myUser:myPassword@myAcc.cloudant.com/myDB"];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:serverURL];
[request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
[request setHTTPMethod:@"POST"];
[request setTimeoutInterval:120.0];
[request setHTTPBody:requestBody];
[NSURLConnection sendAsynchronousRequest:request queue:[[NSOperationQueue alloc] init] completionHandler:^(NSURLResponse *response, NSData *data, NSError *error)
{
if (response != nil) {
if ([response respondsToSelector:@selector(statusCode)])
{
int statusCode = [((NSHTTPURLResponse *)response) statusCode];
NSLog(@"StatusCode returned was %i", statusCode);
}
NSLog(@"Response: %@",response);
}
if ([data length] >0 && error == nil)
{
NSLog(@"data: %@", data);
// DO YOUR WORK HERE
}
else if ([data length] == 0 && error == nil)
{
NSLog(@"Nothing was downloaded.");
}
else if (error != nil){
NSLog(@"Error = %@", error);
}
}];
编辑:超时问题仍然存在。我在 didSendBodyData: 中记录了进度,并认识到该字节已发送。不是全部,然后超时发生。使用 curl,上传工作正常。
另外,我在日志中发现了一件事:请求成功时,总字节数和预期字节数相同:
Total Bytes written: 161120
Expected Bytes to write: 161120
When the request failed:
Total Bytes written: 163840
Expected Bytes to write: 166839
还有其他想法吗?
- (void)post:(NSData*) requestBody {
NSURL* serverURL = [NSURL URLWithString: @"https://mySpace.cloudant.com/myDb"];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:serverURL
cachePolicy:NSURLRequestUseProtocolCachePolicy
timeoutInterval:30.0];
[request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
[request setHTTPMethod:@"POST"];
[request setHTTPBody:requestBody];
NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
[connection start];
}
#pragma mark -
#pragma mark Connection Delegates
- (void) connection:(NSURLConnection *)connection didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge
{
if ([challenge previousFailureCount] == 0) {
NSLog(@"received authentication challenge");
NSURLCredential *newCredential = [NSURLCredential credentialWithUser:@"myUser"
password:@"myPwd"
persistence:NSURLCredentialPersistenceForSession];
NSLog(@"credential created");
[[challenge sender] useCredential:newCredential forAuthenticationChallenge:challenge];
NSLog(@"responded to authentication challenge");
}
else {
NSLog(@"previous authentication failure");
}
}
【问题讨论】:
-
检查你的服务器配置而不是 iOS。如果有限制,则在服务器端设置。
-
有几件事可能会导致这种情况。我怀疑您是否达到了任何请求大小限制(我认为 Cloudant 中的最大文档大小约为 64MB)。我的第一步是看看你是否可以使用例如重现问题。卷曲。如果您仍然遇到超时,请发送电子邮件至 support@cloudant.com,我们将与您一起解决问题。