1

更新:顯然,在iOS 5上,問題在於「分塊編碼」,發送時沒有一切正常。似乎在服務器上,出於某種原因在iOS 5上的傳輸永遠不會結束(在iOS 6上一切正常)。任何人都有辦法解決這個問題?在iOS 5中使用NSURLConnection無法正常工作


我使用NSURLConnection的這完美的作品在iOS 6上同一版本的模擬器,但是測試,在較早的設備,當我得到響應,只有

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

,且從未

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data 

假設包含我的相關數據。

下面是我用我用過的所有功能代碼的片段(我看到一些人消除一些委託功能解決了類似的問題,但對我來說,我沒有他們):

-(void)establishConnection{ 

NSURL *url; 

url = .... // Here I've set my url - it's https 


self.responseData = [[NSMutableData alloc] initWithLength:0] ; 
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:SERVER_RESPONSE_TIMEOUT]; 

[request setHTTPMethod:@"POST"]; 

// More settings here // 
.... 


//Accept-Language: ENUS 
[request addValue:@"ENUS" forHTTPHeaderField:@"Accept-Language"]; 

// "Accept-Topic: Dictation" 
[request addValue:@"Dictation" forHTTPHeaderField:@"Accept-Topic"]; 

// "Accept: text/plain" 
[request addValue:@"text/plain" forHTTPHeaderField:@"Accept"]; 

//"Transfer-Encoding: chunked" 
[request addValue:@"chunked" forHTTPHeaderField:@"Transfer-Encoding"]; 

NSMutableData *postBody = [NSMutableData data]; 

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
NSString *documentsDirectory = [NSString stringWithFormat:@"%@",[paths objectAtIndex:0]]; // Get sound directory 
NSData *soundData = [NSData dataWithContentsOfFile: [NSString stringWithFormat:@"%@/%@",documentsDirectory, @"rec.wav"]]; 

[postBody appendData:soundData]; 
[postBody appendData:[@"\r\n" dataUsingEncoding: NSUTF8StringEncoding]]; 

// final boundary 
//[postBody appendData:[[NSString stringWithFormat:@"--%@\r\n", stringBoundary] dataUsingEncoding:NSUTF8StringEncoding]]; 

// add body to post 
[request setHTTPBody:postBody]; 

self.connection = [[NSURLConnection alloc] initWithRequest:request delegate:self startImmediately:YES]; 

} 

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response { 
// You may have received an HTTP 200 here, or not... 
NSLog(@"didReceiveResponse"); 

} 

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data { 

NSString* aStr = [[NSString alloc] initWithData:data encoding:NSASCIIStringEncoding]; 

NSLog(@"This is my first chunk %@", aStr); 

} 

- (void)connectionDidFinishLoading:(NSURLConnection *)connectionV { 
connectionV = nil; 
} 

- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error { 
NSLog(@"Something went wrong..."); 

} 

請幫助我找不到我做錯了什麼。

+1

您在didReceiveResponse中收到200嗎? –

+0

不,我得到400.因爲https,iOS5有什麼不同嗎?它適用於iOS 6 ... 我發送兩個完全相同的參數。 – Idan

+0

似乎問題與iOS 5上的「分塊編碼」有關。請檢出我的更新。 – Idan

回答

0

您不應該自己設置Transfer-Encoding chunked。 NSURLConnection會在適當時爲您設置。

基本上,HTTP消息需要一個Content-Length頭部集合,或者它使用分塊傳輸編碼,其中不需要設置Content-Length頭部。

當您通過申請財產HTTPBodyStream設置體數據作爲,不明確指定的內容長度,當身體數據流上的狀態下結束NSURLConnection會自動使用分塊傳輸編碼和基礎決定(檢測EOF)。

否則,如果您有NSData對象通過屬性HTTPBody定身的數據,你可以明確的設置內容長度,或者讓NSURLConnection組爲您的基礎上,NSData對象的長度。在這種情況下,您不會獲得分塊傳輸編碼。否則,如果您將您的正文數據設置爲流(例如您創建爲文件流的NSInputStream),並明確設置Content-Length標頭,NSURLConnection將不使用分塊傳輸編碼。

如果可能,即使是NSInputStream也要設置Content-Length,也就是說,當您能夠事先知道身體有多大時。可能有服務器遇到麻煩或者根本不能解析通過組塊傳輸編碼傳輸的數據,例如,在發送JSON或XML數據時,使用「簡單服務器」(比如WEBrick)。否則,Web服務器將緩衝所有輸入數據。

相關問題