在iOS 5中使用NSURLConnection无法正常工作
更新:显然,在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...");
}
请帮助我找不到我做错了什么。
您不应该自己设置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服务器将缓冲所有输入数据。
您在didReceiveResponse中收到200吗? –
不,我得到400.因为https,iOS5有什么不同吗?它适用于iOS 6 ... 我发送两个完全相同的参数。 – Idan
似乎问题与iOS 5上的“分块编码”有关。请检出我的更新。 – Idan