3

I'm trying to get source code of this page (http://www.sanmarinocard.sm) but dataWithContentsOfURL doesn't retrieves data.

NSURL *url = [NSURL URLWithString:@"http://www.sanmarinocard.sm"];
NSData *responseData = [NSData dataWithContentsOfURL:url];
NSString *result = [[NSString alloc] initWithData:responseData encoding:NSASCIIStringEncoding];

Until a few days ago everything worked fine, but since yesterday no results.

Can you help me? Thank you.

Gigi
  • 315
  • 7
  • 23

3 Answers3

5

Yes, it's working for me as well. Perhaps the server was temporarily unavailable.

It's not recommend to use dataWithContentsOfURL: as it's synchronous and will block your app will working; if the server is not responding it could block your app for quite a long time.

The preferred method would be to use NSMutableURLRequest and send an asynchronous request. This will allow you app to remain responsive whilst it is loading, and also let you set a timeout interval and handle any errors more easily.

NSURL *url = [NSURL URLWithString:@"http://www.sanmarinocard.sm"];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
[request setTimeoutInterval: 10.0]; // Will timeout after 10 seconds
[NSURLConnection sendAsynchronousRequest:request
                                   queue:[NSOperationQueue currentQueue]
                       completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) {

    if (data != nil && error == nil)
    {
        NSString *sourceHTML = [[NSString alloc] initWithData:data];
        // It worked, your source HTML is in sourceHTML
    }
    else
    {
        // There was an error, alert the user
    }

   }];
petemorris
  • 516
  • 4
  • 4
3

I discovered that my application is blocked from the site by filtering the user-agent. I changed the value and now it works perfectly.

I modified the code like this:

NSString *userAgent = @"Mozilla/4.0 (compatible; MSIE 5.23; Mac_PowerPC)";
NSURL *url = [NSURL URLWithString: @"http://www.sanmarinocard.sm"];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc]initWithURL:url];
[request setValue:userAgent forHTTPHeaderField:@"User-Agent"];
NSURLResponse *response = nil;
NSError *error = nil;
NSData *responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
NSString *result = [[NSString alloc] initWithData:responseData encoding:NSASCIIStringEncoding];

Thank you!

Gigi
  • 315
  • 7
  • 23
1

Now, it works fine. Maybe the error happened due to temporary server unavailability.

Moreover, as mentioned before, using NSUTF8StringEncoding is highly recommend.

EDIT:

It can also be in a relation with caching: dataWithContentsOfURL: uses NSURLConnection under the hood, which caches responses by default. See this question how to ignore it.

Community
  • 1
  • 1
Attila H
  • 3,616
  • 2
  • 24
  • 37
  • Damn! I noticed that the project of my application does not work. But if I use the same code in a new project work. Is it possible that the application has been blocked? – Gigi Jan 20 '13 at 12:19
  • Does it work if you try downloading a webpage other than you specified in the example code? – Attila H Jan 20 '13 at 16:32
  • Is It possible that a server-agent filter has been installed on the site http://www.sanmarinocard.sm? – Gigi Jan 21 '13 at 07:30
  • Don't know. Take a loot at my edited answer. I've found a possible reason for you case! – Attila H Jan 21 '13 at 08:28