1

I am trying to setup a http proxy server in my computer to simulate a virtual development environment.

To access the local url, I put the DNS info into computers' /etc/hosts.

Here is my test code:

NSURLSession *session = [NSURLSession sharedSession];
NSURL *url = [NSURL URLWithString:@"http://someurl.local/bluh/bluh/path"];
NSURLSessionDataTask *dataTask =
  [session dataTaskWithURL:url
         completionHandler:^(NSData *data, NSURLResponse *response,
                             NSError *error) {
             NSLog(@"data is %@ error is %@", data, error);
         }];
[dataTask resume];

My Experiments: 1. Browsers like safari or chrome can access both local web services and remote web services. 2. NSURLSession will response a NSURLErrorDomain Code=-1001 "The request timed out." when access local urls. 3. NSURLSession works correct if url not in /etc/hosts.

I also tried the NSURLConnection, same output as NSURLSession's. so, is it means the NSURLConnection/NSURLSession and browsers use a different strategy to do the DNS?

how to deal or bypass this?

update

I build this environment for ios 7.0+.

Environment:

  • computer system: OS X 10.9.4 (13E28) which set up a proxy and have server in.
  • device system: iOS 7.1.2(11D257)
chao787
  • 1,760
  • 3
  • 18
  • 20
  • 1
    You might want to make it clearer where you're running this code. Is this built for OS X? Are you running it on your iPhone? (The above code would compile for either...) Can we see your hosts entry? And have you tried with a different domain name? `.local` has a specific meaning to OS X, I believe. I use `.localhost` and this kind of thing works fine for me. – Matt Gibson Aug 13 '14 at 07:02
  • @MattGibson Solved this after replace `.local` to `.localhost`. So do you mind to answer it formally? – chao787 Aug 13 '14 at 07:57
  • Really lucky for not masking the Top level domain... – chao787 Aug 13 '14 at 08:00
  • Done. Yeah, that would have been a lot more of a stumper without the real TLD. – Matt Gibson Aug 13 '14 at 08:04

1 Answers1

1

.local is treated as a special TLD on OS X, and I think iOS. I believe this is primarily because it's used as an indicator for Bonjour lookups. Try changing to a different TLD for your development domains. I use ".localhost"; I've seen others use ".dev". Both of those work as expected.

It looks from the linked article that you might have been running into the problem specifically because you were using x.local—a single label, which will be looked up via Bonjour instead of by DNS, so you might also find that x.y.local works, too, but personally I'd just avoid .local all together.

Matt Gibson
  • 37,886
  • 9
  • 99
  • 128
  • We have exactly the same situation: a server running on a local Mac is accessible from itself, a neighbouring Mac on the same network and Mobile Safari on the same network by its `foo.local` address. But attempting to connect to `foo.local` from `NSURLSession` running on the phone results in a timeout. This answer doesn't tell me much—why doesn't it work? The fact that it's Bonjour or DNS doing the lookup should be transparent to `NSURLSession`, shouldn't it? – Robert Atkins Aug 10 '16 at 08:57