问题
This is officially the weirdest thing I've ever seen. I have an iPhone 5 and an iPhone 5s, both on iOS 7.1.2, completely clean. I run the following code:
NSString *userAgent = @"Mozilla/5.0 (iPhone; CPU iPhone OS 5_0 like Mac OS X) AppleWebKit/534.46 (KHTML, like Gecko) Version/5.1 Mobile/9A334 Safari/7534.48.3";
NSURL *url = [NSURL URLWithString: deviceString];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc]initWithURL:url];
[request setValue:userAgent forHTTPHeaderField:@"User-Agent"];
NSHTTPURLResponse *response = nil;
NSError *error = nil;
NSData *responseData = [NSURLConnection sendSynchronousRequest:request 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]);
The iPhone 5s returns the correct page source that I am looking for, and the same one I get from the simulator. The header looks like this:
"Alternate-Protocol" = "80:quic";
"Cache-Control" = "no-cache";
"Content-Encoding" = gzip;
"Content-Length" = 5627;
"Content-Type" = "text/plain; charset=utf-8";
Date = "Fri, 01 Aug 2014 17:21:40 GMT";
Expires = "Tue, 27 Apr 1971 19:44:06 EST";
P3P = "CP=\"This is not a P3P policy! See http://support.google.com/accounts/bin/answer.py?answer=151657&hl=en for more info.\"";
Server = "gwiseguy/2.0";
"Set-Cookie" = "YSC=oohlyqgkgwg; path=/; domain=.youtube.com; httponly";
"X-Content-Type-Options" = nosniff;
"X-Frame-Options" = SAMEORIGIN;
"X-XSS-Protection" = "1; mode=block; report=https://www.google.com/appserve/security-bugs/log/youtube";
The iPhone 5 returns something different, and significantly shorter as you can see from the Content-Length header:
"Alternate-Protocol" = "80:quic";
"Cache-Control" = "no-cache";
"Content-Length" = 767;
"Content-Type" = "text/html; charset=utf-8";
Date = "Fri, 01 Aug 2014 17:23:06 GMT";
Expires = "Tue, 27 Apr 1971 19:44:06 EST";
P3P = "CP=\"This is not a P3P policy! See http://support.google.com/accounts/bin/answer.py?answer=151657&hl=en for more info.\"";
Server = "gwiseguy/2.0";
"Set-Cookie" = "YSC=2KFSqyO4p1k; path=/; domain=.youtube.com; httponly";
"X-Content-Type-Options" = nosniff;
"X-Frame-Options" = SAMEORIGIN;
"X-XSS-Protection" = "1; mode=block; report=https://www.google.com/appserve/security-bugs/log/youtube";
I'm confused.
EDIT:
I have now discovered an iPhone 5 that is working properly and an iPhone 5s that is not!? We are testing on the same wifi network but the phones are split between AT&T and Verizon service. 1 Verizon is working, the others are not. All AT&T is working.
UPDATE:
I have solved the problem. Now i need some help understanding what I did :/ I noticed the difference between the headers was the line "Content-Encoding" = gzip
So I set this line in my code:
[request setValue:@"gzip" forHTTPHeaderField:@"Content-Encoding"];
Now the request works as expected, but why do I need to do that? I haven't in the past, this is not a new feature.
回答1:
I noticed the difference between the headers was the line "Content-Encoding" = gzip So I set this line in my code:
[request setValue:@"gzip" forHTTPHeaderField:@"Content-Encoding"];
Now the request works as expected.
来源:https://stackoverflow.com/questions/25085595/nshttpurlresponse-different-on-iphone-5-vs-5s