我尝试使用NSURLConnection sendSynchronousRequest:将二进制文件(JPEG图像)发送到服务器。我的代码是:
// url contains the URL of the remote server.
// fileURL contains the URL for the local file to be sent.
request = [NSMutableURLRequest requestWithURL:url];
[request setCachePolicy:NSURLRequestReloadIgnoringLocalCacheData];
[request setHTTPMethod:@"PUT"];
[request setValue:@"application/json" forHTTPHeaderField:@"Accept"];
[request setValue:@"image/jpeg" forHTTPHeaderField:@"Content-Type"];
[request setHTTPBodyStream:[NSInputStream inputStreamWithURL:fileURL]];
responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&requestError];
if (requestError != nil) {
NSLog(@"%@", requestError);
return NO;
}
这几乎可行。远程服务器正确接收请求,并使用适当的JSON对象进行响应。但是sendSynchronousRequest:阻塞直到超时值,然后报告超时。
在其他地方,我能够使用相同的代码模式正确地发送和接收JSON对象,所以我很困惑为什么这个会超时。