ios - iPhone 5 和 5s 上的 NSHTTPURLResponse 不同

标签 ios nsurlconnection iphone-5 nshttpurlresponse

这正式是我见过的最奇怪的事情。我有一部 iPhone 5 和一部 iPhone 5s,都在 iOS 7.1.2 上,完全干净。我运行以下代码:

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]);

iPhone 5s 返回我正在寻找的正确页面源,与我从模拟器中获得的相同。 header 如下所示:

"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";

iPhone 5 返回一些不同的东西,并且从 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";

我很困惑。

编辑:

我现在发现 iPhone 5 可以正常工作而 iPhone 5s 不能正常工作!?我们正在同一个 wifi 网络上进行测试,但电话在 AT&T 和 Verizon 服务之间分配。 1 Verizon 正在运行,其他则没有。所有 AT&T 都在工作。

更新:

我已经解决了这个问题。现在我需要一些帮助来理解我做了什么:/我注意到 header 之间的区别是行 "Content-Encoding"= gzip 所以我在我的代码中设置了这一行:

    [request setValue:@"gzip" forHTTPHeaderField:@"Content-Encoding"];

现在请求按预期工作,但我为什么需要这样做?我过去没有,这不是新功能。

最佳答案

我注意到 header 之间的区别在于“Content-Encoding”= gzip 这行所以我在我的代码中设置了这一行:

 [request setValue:@"gzip" forHTTPHeaderField:@"Content-Encoding"];

现在请求按预期工作。

关于ios - iPhone 5 和 5s 上的 NSHTTPURLResponse 不同,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25085595/

相关文章:

ios - 调整字体大小以适合标签高度 - Swift 4、Xcode 9

ios - 从现有应用程序中排除 iPad 支持

ios - 适用于 iPhone 5 Retina 4 的 MonoTouch : Adding Default-568h@2x. png

ios - 在新 iPad 上运行 iOS6 应用程序

iOS App > 无法设置远程报价 sdp : Called with SDP without DTLS fingerprint

ios - Swift Realm - 如何计算单列中的所有数据?

iOS:为什么我需要在 NSURLConnection sendAsynchronousRequest 之后使用 sleep(1)?

iphone - 从 5 月 1 日起 - iPhone 5 支持 - 那么应用商店支持的最低 iOS 版本是多少?

iphone - 释放 NSURLConnection 对象

objective-c - NSURLConnection 和对 ssl 后面的 Web 服务进行身份验证?