Sometimes I used NSURLSessionTask to post data, task returned timeout. Because my default timeout is 3s, but it took 6s.
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:urlStr] cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:3];
request.HTTPMethod = @"POST";
[request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
[request setValue:[@([data length]) stringValue] forHTTPHeaderField:@"Content-Length"];
[request setValue:@"OS/macOS OSVersion/10.13.6 AppVersion/1.4.6 DeviceId/8e76a88474ea66e3725f2cdb93a4afdd8d0e5a79c61f15567e8919b5f931c7f0" forHTTPHeaderField: @"User-Agent"];
request.HTTPBody = data;
NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];
NSDictionary *proxyDict = @{
@"HTTPEnable" : [NSNumber numberWithInt:1],
(NSString *)kCFStreamPropertyProxyLocalBypass : @"*.*.*.*",
@"HTTPSEnable" : [NSNumber numberWithInt:1],
(NSString *)kCFStreamPropertyProxyLocalBypass : @"*.*.*.*"
};
configuration.connectionProxyDictionary = proxyDict;
NSURLSession *session = [NSURLSession sessionWithConfiguration:configuration delegate:self delegateQueue:nil];
NSURLSessionTask *task = [session dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error){
[session finishTasksAndInvalidate];
}];
[task resume];
The error occurred after changing the domain name. When I used old domain, it is right.
The error info:
The error info is Error domain=NSURLErrorDomain Code=-1001 "The request timed out." UserInfo={NSUnderlyingError=0x600001536ac0 {Error Domain=kCFErrorDomainCFNetwork Code=-1001 "(null)" UserInfo={_kCFStreamErrorCodeKey=-2102, _kCFStreamErrorDomainKey=4}}, NSErrorFailingURLStringKey=https://mdm.***.com/v0/register, NSErrorFailingURLKey=https://mdm.***.com/v0/register, _kCFStreamErrorDomainKey=4, _kCFStreamErrorCodeKey=-2102, NSLocalizedDescription=The request timed out.}
20:45:16.147871 +0800 testVgun TIC Enabling TLS [2:0x600000169000]
20:45:16.147911 +0800 testVgun TCP Conn [2:0x600000169000] using custom proxy configuration
20:45:16.147966 +0800 testVgun TIC TCP Conn Start [2:0x600000169000]
20:45:16.148107 +0800 testVgun Task <E40048DC-EA15-429D-829B-0C3C2C803B67>.<1> setting up Connection 2
20:45:16.148139 +0800 testVgun [2 <private> <private>] start
20:45:16.160338 +0800 testVgun client.trigger:#N CCFG for cid 0x35 has # of profiles: 0
20:45:16.161538 +0800 testVgun TIC TLS Event [2:0x600000169000]: 1, Pending(0)
20:45:16.162578 +0800 testVgun Received configuration update from daemon (initial)
20:45:16.172504 +0800 testVgun TIC TLS Event [2:0x600000169000]: 2, Pending(0)
20:45:16.173203 +0800 testVgun TIC TLS Event [2:0x600000169000]: 11, Pending(0)
20:45:16.173574 +0800 testVgun TIC TLS Event [2:0x600000169000]: 12, Pending(0)
20:45:16.173612 +0800 testVgun TIC TLS Event [2:0x600000169000]: 14, Pending(0)
20:45:19.660936 +0800 testVgun System Trust Evaluation yielded status(0)
20:45:23.064824 +0800 testVgun TIC TLS Trust Result [2:0x600000169000]: 0
20:45:23.073717 +0800 testVgun TIC TLS Event [2:0x600000169000]: 20, Pending(0)
20:45:23.073757 +0800 testVgun TIC TCP Conn Connected [2:0x600000169000]: Err(16)
20:45:23.073956 +0800 testVgun TIC TCP Conn Event [2:0x600000169000]: 1
20:45:23.073996 +0800 testVgun TIC TCP Conn Event [2:0x600000169000]: 8
20:45:23.074091 +0800 testVgun TIC TLS Handshake Complete [2:0x600000169000]
Then I use NSURLSessionTaskTransactionMetrics to get more details.
20:45:23.097887 +0800 testVgun connectStartDate:[Tue Jan 15 20:45:16 2019], connectEndDate:[Tue Jan 15 20:45:23 2019]
20:45:23.097965 +0800 testVgun fetchStartDate:[Tue Jan 15 20:45:16 2019], fetchStartDate:[Tue Jan 15 20:45:16 2019]
20:45:23.098043 +0800 testVgun responseStartDate:[Tue Jan 15 20:45:23 2019], responseEndDate:[Tue Jan 15 20:45:23 2019]
20:45:23.098122 +0800 testVgun requestStartDate:[Tue Jan 15 20:45:23 2019], requestEndDate:[Tue Jan 15 20:45:23 2019]
20:45:23.098200 +0800 testVgun domainLookupStartDate:[Tue Jan 15 20:45:16 2019], domainLookupEndDate:[Tue Jan 15 20:45:16 2019]
20:45:23.098304 +0800 testVgun secureConnectionStartDate:[Tue Jan 15 20:45:16 2019], secureConnectionEndDate:[Tue Jan 15 20:45:23 2019]
The secureConnection took 6+ seconds. This is what causes the timeout.
Maybe verifying certificate took long time.
At connection time, a launchservice called com.apple.mdworker.bundles started.
Finally, I want to know why TLS takes 6+ seconds to connect.
Thanks!