NSMutableURLRequest * request = [NSMutableURLRequestrequestWithURL: self. address cachePolicy: NSURLRequestReloadIgnoringLocalAndRemoteCacheData
TimeoutInterval: self. defaultTimeout];
TimeoutInterval is no longer valid.
This problem exists only in the OS 3.0 and later versions, and timeout will only expire after setHTTPBody is called. This is a change made by Apple to URLLoadingSystem in OS3.0, but in my opinion this is actually a bug! After setHTTPBody, the request timeout is changed to 240 s (this can be viewed through NSLog [requesttimeoutInterval ), apple developers usually set a too short timeout, which usually takes a long time to communicate with the network on mobile devices, if your timeout is 10 s, you may have just "bring WWANInterface up" in the WWAN network environment (I don't know how to translate, translate ). Therefore, if the data of HTTPbody is set after OS 3, the system will automatically set a minimum timeout value (240 s), and this value cannot be changed, that is, if you set timeoutInterval again, you still get 240 S through NSLog [request timeoutInterval !!
The solution I want is to customize only one operation. The method is stupid, but it is really good
NSMutableURLRequest * request = [NSMutableURLRequestrequestWithURL: self. address
CachePolicy: NSURLRequestReloadIgnoringLocalAndRemoteCacheData
TimeoutInterval: self. defaultTimeout];
NSData * bodyData = [outputBodydataUsingEncoding: NSUTF8StringEncoding];
If (cookies! = Nil ){
[RequestsetAllHTTPHeaderFields: [NSHTTPCookierequestHeaderFieldsWithCookies: cookies];
}
[RequestsetValue: @ "wsdl2objc" forHTTPHeaderField: @ "User-Agent"];
[RequestsetValue: soapAction forHTTPHeaderField: @ "SOAPAction"];
[RequestsetValue: @ "text/xml; charset = UTF-8" forHTTPHeaderField: @ "Content-Type"];
[RequestsetValue: [NSString stringWithFormat: @ "% u", [bodyData length] forHTTPHeaderField: @ "Content-Length"];
[RequestsetValue: self. address. host forHTTPHeaderField: @ "Host"];
[RequestsetHTTPMethod: @ "POST"];
[RequestsetHTTPBody: bodyData];
NSURLConnection * connection = [[NSURLConnection alloc] initWithRequest: request delegate: operation];
// Custom time-out
[NSTimerscheduledTimerWithTimeInterval: self. defaultTimeout target: selfselector: @ selector (handleTimer) userInfo: operationrepeats: NO];
Operation. urlConnection = connection;
[Connectionrelease];
}
// Time-out definition
-(Void) handleTimer
{
[OperationCopy connection: [NSError errorWithDomain: @ "time-out! "Code: 256 userInfo: nil];
}
This is an excerpt from the column of the advanced coders.