NSURLConnection failure - 1003

喜欢而已 提交于 2019-12-10 23:19:07

问题


I try to retrive data from certain url with command:

-(NSMutableData *) callUrl: (NSString *)url withData:(NSMutableDictionary *)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.


回答1:


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.



回答2:


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.




回答3:


before making any new connection call cancel previous connection. using

[self.connection cancel];


来源:https://stackoverflow.com/questions/6573324/nsurlconnection-failure-1003

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!