NSURLConnection在多次请求后获得超时

时间:2017-06-29 12:29:32

标签: macos

我的代码如下所示 1)对于每个流媒体请求,我们将在单独的线程中处理请求。

2)如果我创建单独的exe工作正常,吹流。单个exe的问题。如果我为url创建单独的连接流媒体工作正常。

-(void)processRequest:(NSDictionary*)jsonObject
{
   // [NSThread detachNewThreadSelector:@selector(putHttpRequest:) toTarget:self withObject:jsonObject];
    NSInvocationOperation *invocationOP = [[NSInvocationOperation alloc] initWithTarget:myObj selector:@selector(putHttpRequest:) object:jsonObject] ;
    [rpcEventQueue addOperation:invocationOP];
    [invocationOP release];

}

-(void)putHttpRequest:(NSDictionary*)jsonObject
{
    [NSThread detachNewThreadSelector:@selector(databaseQueryForStreaming:) toTarget:self withObject:dict];
}

-(void)databaseQueryForStreaming:(NSDictionary*)dict
{
    [self performHttpPUTRequestForStreamingModifiedOne:dict];
}

-(void)performHttpPUTRequestForStreamingModifiedOne:(NSDictionary*)requestDict
{

NSString *url =@"http://asia.remotepc.com/proxy/host_put_response";
    NSMutableURLRequest * theRequest = (NSMutableURLRequest*)[NSMutableURLRequest requestWithURL:[NSURL URLWithString:url] cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60.0];

    [theRequest setHTTPMethod:@"PUT"];
    [theRequest addValue:request_id forHTTPHeaderField:@"request_id"];
    [theRequest addValue:@"200" forHTTPHeaderField:@"X-StatusCode"];
    [theRequest addValue:[NSString stringWithFormat:@"%ld", responseSize]  forHTTPHeaderField:@"X-ResposeSize"];

        [theRequest addValue:[NSString stringWithFormat:@"%@",fileSize] forHTTPHeaderField:@"X-FileSize"];
    [theRequest addValue:[NSString stringWithFormat:@"%@",lmd] forHTTPHeaderField:@"X-FileLMD"];
    [theRequest setValue:@"video/mp4" forHTTPHeaderField:@"Content-Type"];
    [theRequest setValue:@"chunked" forHTTPHeaderField:@"Transfer-Encoding"];


      NSOperationQueue *senderQueue = [[[NSOperationQueue alloc] init] autorelease];
    RPCStreamReader *stream =[[RPCStreamReader alloc]initWithFileAtPath:filepath];
    [stream setProperty:[NSNumber numberWithLong:start] forKey:NSStreamFileCurrentOffsetKey];
    if (end==1)
    {
        NSFileHandle *handle = [NSFileHandle fileHandleForReadingAtPath:filepath];
        NSData *fileData = [handle readDataOfLength:30000];
        [theRequest setHTTPBodyStream:[NSInputStream inputStreamWithData:fileData]];
        [handle closeFile];
    }
    else
    {
     [theRequest setHTTPBodyStream:stream];
    }

[NSURLConnection sendAsynchronousRequest:theRequest queue:senderQueue
                               completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) {

                                   NSLog(@"Finished with status code: %i and response:%@", [(NSHTTPURLResponse *)response statusCode],[NSHTTPURLResponse localizedStringForStatusCode:[(NSHTTPURLResponse *)response statusCode]]);
                                   NSLog(@"error:%@",error);
                                   if(error)
                                       NSLog(@"Request id:%@ failed",request_id);
                                   else
                                       NSLog(@"Request id:%@ succeeded",request_id);
                               }
         ];

}

1 个答案:

答案 0 :(得分:0)

1)将NSMutableURLRequest时间减少到10秒而不是60秒

2)NSMutableURLRequest发送从异步修改为Syncronous

问题解决了工作正常。