4

I tried to GET html from Apple homepage.

AFHTTPRequestOperationManager *manager
= [AFHTTPRequestOperationManager manager];
[manager GET:@"http://www.apple.com/jp" parameters:nil
     success:^(AFHTTPRequestOperation *operation, id responseObject) {
    NSLog(@"HTML: %@", responseObject);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
    NSLog(@"Error: %@", error);
}];

This failed with error message as below:

2014-02-07 14:54:22.922 Tests[1833:70b] Error: Error Domain=AFNetworkingErrorDomain Code=-1016 "Request failed: unacceptable content-type: text/html" UserInfo=0x8a32650 {NSErrorFailingURLKey=http://www.apple.com/jp/, AFNetworkingOperationFailingURLResponseErrorKey=<NSHTTPURLResponse: 0xea2e1a0> { URL: http://www.apple.com/jp/ } { status code: 200, headers {
    "Accept-Ranges" = bytes;
    "Cache-Control" = "max-age=237";
    Connection = "keep-alive";
    "Content-Encoding" = gzip;
    "Content-Length" = 4402;
    "Content-Type" = "text/html; charset=UTF-8";
    Date = "Fri, 07 Feb 2014 05:52:51 GMT";
    Expires = "Fri, 07 Feb 2014 05:56:48 GMT";
    Server = Apache;
    Vary = "Accept-Encoding";
    Via = "1.0 proxy1.screen.co.jp (squid)";
    "X-Cache" = "MISS from proxy1.screen.co.jp";
    "X-Cache-Lookup" = "HIT from proxy1.screen.co.jp:8080";
} }, NSLocalizedDescription=Request failed: unacceptable content-type: text/html}

I found the message Request failed: unacceptable content-type: text/html so I understand I have to let text/html acceptable. Then, I added the line: manager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/html"];.

AFHTTPRequestOperationManager *manager 
= [AFHTTPRequestOperationManager manager];
manager.responseSerializer.acceptableContentTypes
= [NSSet setWithObject:@"text/html"]; // ###### ADDED LINE ######
[manager GET:@"http://www.apple.com/jp" parameters:nil
     success:^(AFHTTPRequestOperation *operation, id responseObject) {
    NSLog(@"HTML: %@", responseObject);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
    NSLog(@"Error: %@", error);
}];

But this failed, too.

2014-02-07 14:55:48.927 Tests[1873:70b]
Error: Error Domain=NSCocoaErrorDomain Code=3840
"The operation couldn’t be completed. (Cocoa error 3840.)"
(JSON text did not start with
array or object and option to allow fragments not set.)
UserInfo=0xfa7b870 {NSDebugDescription=
JSON text did not start with
array or object and option to allow fragments not set.}

Thank you for your help.

Feel Physics
  • 2,783
  • 4
  • 25
  • 38

2 Answers2

16

The problem is that AFHTTPRequestOperationManager defaults to a responseSerializer of AFJSONResponseSerializer. By changing acceptableContentTypes, you are changing which content types will be accepted, but you're not changing the fact that AFJSONResponseSerializer will still look for JSON in the response.

You might want to just set the responseSerializer to AFHTTPResponseSerializer, so that it will no longer try to parse the JSON (and will automatically accept text/html):

manager.responseSerializer = [AFHTTPResponseSerializer serializer];
Rob
  • 415,655
  • 72
  • 787
  • 1,044
  • I also have to chage the line:`NSLog(@"HTML: %@", [responseObject stringValue]);` – Feel Physics Feb 10 '14 at 02:22
  • I found more simple way: `NSError* error = [[NSError alloc] init];NSString* content = [NSString stringWithContentsOfURL:[NSURL URLWithString:@"http://www.apple.co.jp/"] encoding:NSUTF8StringEncoding error:&error];` I don't have to use AFNetworking... – Feel Physics Feb 10 '14 at 02:52
  • 3
    @weed That's fine, if that works for you, but note that that's a synchronous call, which you should never do from the main thread. If you dispatch this code to a background queue, then that's ok (but still loses some benefits of AFNetworking or a delegate-based NSURLConnection, e.g. cancelable requests). Bottom line, don't use AFNetworking if you don't want, but I hate to see you throw the baby out with the bath water. – Rob Feb 10 '14 at 05:31
  • Thank you for your advice, Rob. I'm going to try AFNetworking. – Feel Physics Feb 28 '14 at 01:37
  • I THINK YOU NAILED IT BRO! Thank you so Much! Hey so Do I Have to change the responseSerializer to a previous Value? – Hernan Arber Aug 09 '14 at 00:28
0

I just tried with wget, and ending with "jp" gave a 301 redirect.

Try ending it with "jp/".

John Visosky
  • 237
  • 1
  • 1