开发者

NSURLConnection failure - 1003

开发者 https://www.devze.com 2023-03-17 17:24 出处:网络
I try to retrive data from certain url with command: -(NSMutableData *) callUrl: (NSString *)url withData:(N开发者_开发问答SMutableDictionary *)data delegate:(id) delegate {

I try to retrive data from certain url with command:

-(NSMutableData *) callUrl: (NSString *)url withData:(N开发者_开发问答SMutableDictionary *)data delegate:(id) delegate {

    NSURL *executeUrl = [NSURL URLWithString:<string>];
    NSURLRequest *request = [NSURLRequest requestWithURL: executeUrl
                             cachePolicy:NSURLRequestReloadIgnoringLocalCacheData
                             timeoutInterval:60];

    NSMutableData *receivedData = nil;

    NSURLConnection *theConnection=[[NSURLConnection alloc] initWithRequest:request delegate:delegate];
    if (theConnection) {
        receivedData = [[NSMutableData data] retain];
    } else {
        @throw @"Connection error";
    }

    return receivedData;
}

In delegate (both after connectionDidFinish and connectionDidFailWithError) I do:

//some uninvasive alerts
// release the connection, and the data object
[connection release];
[receivedData release];

Problem is when I provide bad url I got proper error - it's good part - but then I want to execute second url - good for sure, I've got 1003 error - NSURLErrorCannotFindHost.

After around 1-2 min I'm succesfully call url and get data. I suspect some timeouts and ports business, but changing timeout in NSURLRequest doesn't change a thing.

UPDATE

As it turned out - Administrators had some issues with DNS server reached through WiFi network. Code is fine. Thanks for response. If some has similiar problems: try ip address instead of hostname.


From Apple iOS developer documentation, 1003 error refers to when the host name for a URL cannot be resolved. To avoid DNS failures in wifi, overloaded DNS scenarios, it is preferable to resolve ip from hostname for subsequent use or to hardcode the ip address directly, if you do not intend to shift the hosting later on.

Apple documentation:

URL Loading System Error Codes

These values are returned as the error code property of an NSError object with the domain “NSURLErrorDomain”.

enum
 { 
  NSURLErrorBadURL = -1000,
  NSURLErrorTimedOut = -1001,
  NSURLErrorUnsupportedURL = -1002,
  NSURLErrorCannotFindHost = -1003,//****
  NSURLErrorCannotConnectToHost = -1004,
  NSURLErrorDataLengthExceedsMaximum = -1103,
  NSURLErrorNetworkConnectionLost = -1005,
  NSURLErrorDNSLookupFailed = -1006,
  ...
  }

1003 NSURLErrorCannotFindHost
Returned when the host name for a URL cannot be resolved.
Available in iOS 2.0 and later.
Declared in NSURLError.h.


I did 2 things to fix this issue :

  1. I used this before initiating my NSUrlConnection

    [NSURLConnection cancelPreviousPerformRequestsWithTarget:self];

  2. I changed my DNS to 8.8.8.8 in wifi settings which is google's public DNS server.

Don't know which one fixed it but the issue was resolved.


before making any new connection call cancel previous connection. using

[self.connection cancel];
0

精彩评论

暂无评论...
验证码 换一张
取 消