Iphone solves the problem of NSURLConnection timeout failure

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.