好的,我已经想通了。我用了
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response{
NSDictionary *responseHeaders = ((NSHTTPURLResponse *)response).allHeaderFields;
NSLog(@"headers: %@", responseHeaders.description);
}
这种方法可以找出我的 iOS 应用程序正在接收的标头,并将它们与连接到同一 URL 时 curl 打印的标头进行比较。事实证明,curl 显示的标题与 Objective-C 显示的标题之间存在差异。诚然,这让我很困惑,我不知道为什么会发生这种情况,但我找到了一个解决方案:
ob_start('scAPIHandler');
function scAPIHandler($buffer){
header('Custom-Content-Length: '.strlen($buffer));
return $buffer;
}
Custom-Content-Length 确实出现在 Objective-C 中,我只是拿了我的自定义标题
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response{
self.expectedContentLength = response.expectedContentLength;
self.receivedData.length = 0;
NSDictionary *responseHeaders = ((NSHTTPURLResponse *)response).allHeaderFields;
if(self.expectedContentLength < 0){
// take my own header
NSString *actualContentLength = responseHeaders[@"Custom-Content-Length"];
if(actualContentLength && actualContentLength.length > 0){
self.expectedContentLength = actualContentLength.longLongValue;
}
}
}
并覆盖了预期的内容长度。