Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php NSURLResponse expectedContentLength-1_Php_Objective C_Http Headers_Nsurlconnection_Content Length - Fatal编程技术网

Php NSURLResponse expectedContentLength-1

Php NSURLResponse expectedContentLength-1,php,objective-c,http-headers,nsurlconnection,content-length,Php,Objective C,Http Headers,Nsurlconnection,Content Length,我有一个NSURLConnection,它连接到我制作的PHP API。PHP API用数据进行响应。由于它是一个动态应用程序,我这样做是为了告诉内容长度: ob_start('scAPIHandler'); function scAPIHandler($buffer){ header('Content-Length: '.strlen($buffer)); return $buffer; } 在API文件的开头,该文件随后会输出任何必要的内容。然而,当 - (

我有一个NSURLConnection,它连接到我制作的PHP API。PHP API用数据进行响应。由于它是一个动态应用程序,我这样做是为了告诉内容长度:

ob_start('scAPIHandler');    

function scAPIHandler($buffer){

    header('Content-Length: '.strlen($buffer));

    return $buffer;

}
在API文件的开头,该文件随后会输出任何必要的内容。然而,当

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response 
函数激发时,
response.expectedContentLength
的值为-1。当我尝试连接的不是我的PHP API,而是web上的一些图像时,会显示正确的预期内容长度。有没有办法让我的PHP API告诉NSURL响应预期的内容长度?内容长度标题似乎不起作用


提前谢谢

好的,我已经弄明白了。我曾经

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response{

    NSDictionary *responseHeaders = ((NSHTTPURLResponse *)response).allHeaderFields;

    NSLog(@"headers: %@", responseHeaders.description);

}
此方法用于计算我的iOS应用程序正在接收的头,并将它们与连接到同一URL时打印的头进行比较。事实证明,curl显示的标题和Objective-C显示的标题之间存在差异。诚然,这让我非常困惑,我不知道为什么会发生这种情况,但我找到了一个解决办法:

ob_start('scAPIHandler');    

function scAPIHandler($buffer){

    header('Custom-Content-Length: '.strlen($buffer));

    return $buffer;

}
自定义内容长度确实出现在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;

        }

    }

}
-(void)连接:(NSURLConnection*)连接didReceiverResponse:(nsurResponse*)响应{
self.expectedContentLength=response.expectedContentLength;
self.receivedData.length=0;
NSDictionary*responseHeaders=((NSHTTPPURLResponse*)响应)。所有HeaderFields;
if(self.expectedContentLength<0){
//拿我自己的头球
NSString*actualContentLength=响应标题[@“自定义内容长度”];
如果(实际内容长度和&实际内容长度.length>0){
self.expectedContentLength=实际ContentLength.LongValue;
}
}
}

并重写预期的内容长度。

好的,我已经计算出来了。我曾经

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response{

    NSDictionary *responseHeaders = ((NSHTTPURLResponse *)response).allHeaderFields;

    NSLog(@"headers: %@", responseHeaders.description);

}
此方法用于计算我的iOS应用程序正在接收的头,并将它们与连接到同一URL时打印的头进行比较。事实证明,curl显示的标题和Objective-C显示的标题之间存在差异。诚然,这让我非常困惑,我不知道为什么会发生这种情况,但我找到了一个解决办法:

ob_start('scAPIHandler');    

function scAPIHandler($buffer){

    header('Custom-Content-Length: '.strlen($buffer));

    return $buffer;

}
自定义内容长度确实出现在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;

        }

    }

}
-(void)连接:(NSURLConnection*)连接didReceiverResponse:(nsurResponse*)响应{
self.expectedContentLength=response.expectedContentLength;
self.receivedData.length=0;
NSDictionary*responseHeaders=((NSHTTPPURLResponse*)响应)。所有HeaderFields;
if(self.expectedContentLength<0){
//拿我自己的头球
NSString*actualContentLength=响应标题[@“自定义内容长度”];
如果(实际内容长度和&实际内容长度.length>0){
self.expectedContentLength=实际ContentLength.LongValue;
}
}
}
并重写预期的内容长度