NSData for NSURLConnection return NULL on some sites

I am new to internet connection for iOS. I am trying to get data from a special site.

The following codes work perfectly with the whole site to see the data in the URL.

However, if I change it to a special site to get their data, it returns NULL !!

I think the site is somehow blocking this type of implementation. Because the site contains some XML information.

NSString *URLString = @"http://www.specialsite.com/"; NSURL *postURL = [NSURL URLWithString:URLString]; NSURLRequest *postRequest = [NSURLRequest requestWithURL:postURL]; NSURLResponse *response = nil; NSError *error = nil; NSData *responseData = [NSURLConnection sendSynchronousRequest:postRequest returningResponse:&response error:&error]; if (responseData) { NSLog(@"Response was %@", [NSString stringWithCString:[responseData bytes] encoding:NSUTF8StringEncoding]); } 

Also the answer on the console:

 2011-12-26 12:24:37.245 Arz[6113:207] Response was (null) 

Remember, if I changed:

  NSString *URLString = @"http://www.specialsite.com/"; 

to

  NSString *URLString = @"http://www.apple.com/"; 

working prefect!

Sorry, I can’t name the site here (publicly).

+4
source share
1 answer

You can try using a convenient method:

 NSString *mydata = [[NSString alloc] initWithContentsOfURL:@"http://example.com"]; 

If this does not work, I would get the response headers using curl from the terminal and see what happens.

 curl -I http://www.apple.com 

Alternatively, you can print response headers only inside Xcode when using NSHTTPURLResponse:

 NSURLRequest *postRequest = [NSURLRequest requestWithURL:[NSURL URLWithString: @"http://www.apple.com"]]; NSHTTPURLResponse *response = nil; NSError *error = nil; NSData *responseData = [NSURLConnection sendSynchronousRequest:postRequest returningResponse:&response error:&error]; NSDictionary *dict = [response allHeaderFields]; NSLog(@"Status code: %d",[response statusCode]); NSLog(@"Headers:\n %@",dict.description); NSLog(@"Error: %@",error.description); //NSLog(@"Response data: %@",[[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding]); 

Look for the response code, for example. HTTP / 1.1 200 OK

The response code and headers tell you what is happening from a server perspective. If the response code is no more than 200, you will not receive the expected data. Here you can find a list of HTTP status codes: http://en.wikipedia.org/wiki/List_of_HTTP_status_codes

+5
source

Source: https://habr.com/ru/post/1387994/


All Articles