ios - 每个 "POST"异步请求是否有任何文档大小限制?

标签 ios couchdb cloudant

我正在使用包含数百个位置点的文档设置异步发布请求。我想将此文档保存在我的 couchdb 中。

对于小文档,请求响应在很短的时间内返回,存储文档。当文档增长更多(仍然<~200k)时,响应需要很长时间,然后返回超时。我将超时设置为 120 秒。

我是否缺少某些设置或出现编码错误?

- (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");
    }
}

最佳答案

这似乎是 URL 格式的问题。使用低级 HTTP 库时,您必须手动添加基本身份验证 header - NSURLConnection and Basic HTTP Authentication in iOS展示了如何做到这一点。超时很可能是由于设备试图联系无效的主机。

您可以通过尝试直接使用 curl 发出请求来测试这一点,即:

curl -XPOST https://username.password@username.cloudant.com/db -H 'Content-Type:application/json' -d @test.json

其中 test.json 包含正在超时的 JSON。

使用 curl 可以更轻松地调试(您将看到原始响应),并且还可以确认它是服务器端问题还是特定于库的问题。

关于ios - 每个 "POST"异步请求是否有任何文档大小限制?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19202517/

相关文章:

jquery - 通过 jquery.couch.js 或其他方式在 Couchapp/CouchDB 中进行用户注册

couchdb - 智能代理在 CouchDB 上提供过滤 View

javascript - 如何在没有文档的情况下学习 CouchDB API?

couchdb - 如何触发或检查链式映射缩减(dbcopy)的状态

couchdb - Cloudant:如何为 "Sort"函数创建索引?

iphone - 关闭 UIImagePicker 后获取视频长度?

iphone - 如何在通用 UIViewController - Universal App 中仅为 iPAD 创建 IBOutlet

mapreduce - 选择不同的计数 cloudant/couchdb

iphone - 无法在设备上测试 iOS 应用内购买,只有模拟器

ios - Swift:NSJSONSerialization.JSONObjectWithData 返回 nil 但 json 似乎没问题