我基本上試圖下載一個文件,可以從PDF到PNG。我正在使用AFNetworking 1.x.AFNetworking - 下載文件,意外的消息格式'原始'
不過,我得到一個錯誤:
NSURL *baseURL = [NSURL URLWithString:@"https://myserver.com/webservices/Services/"];
AFHTTPClient *httpClient = [[AFHTTPClient alloc] initWithBaseURL:baseURL];
//NSLog(@"Formatted URL: %@", formattedURL);
//NSMutableURLRequest *photoRequest = [NSMutableURLRequest requestWithURL:url];
NSMutableDictionary *parameters = [[NSMutableDictionary alloc] init];
[httpClient defaultValueForHeader:@"Accept"];
[httpClient setParameterEncoding:AFFormURLParameterEncoding];
[parameters setObject:[NSNumber numberWithInteger:@"10772"] forKey:@"FileId"];
[parameters setObject:[NSNumber numberWithBool:YES] forKey:@"requireUnCompression"];
NSMutableURLRequest *fileRequest = [httpClient requestWithMethod:@"POST" path:@"FileService.svc/DownloadFile" parameters:parameters];
//[fileRequest setHTTPBody:<#(NSData *)#>]
AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:fileRequest];
[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(@"Successss!");
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"Failed: %@", [operation error]);
}];
[httpClient enqueueHTTPRequestOperation:operation];
不過,我得到一個錯誤:
Failed: Error Domain=AFNetworkingErrorDomain Code=-1011 "Expected status code in (200-299), got 400" UserInfo=0x12112770 {NSLocalizedRecoverySuggestion={"faultType":"InvalidOperationException","message":"The incoming message has an unexpected message format 'Raw'. The expected message formats for the operation are 'Xml', 'Json'. This can be because a WebContentTypeMapper has not been configured on the binding. See the documentation of WebContentTypeMapper for more details."}, AFNetworkingOperationFailingURLRequestErrorKey=<NSMutableURLRequest: 0xc865c40> { URL: https://myserver.com/webservice/Services/FileService.svc/DownloadFile }, NSErrorFailingURLKey=https://myserver.com/webservices/Services/FileService.svc/DownloadFile, NSLocalizedDescription=Expected status code in (200-299), got 400, AFNetworkingOperationFailingURLResponseErrorKey=<NSHTTPURLResponse: 0xc8696a0> { URL: https://myserver.com/webservices/Services/FileService.svc/DownloadFile } { status code: 400, headers { "Cache-Control" = private; "Content-Length" = 327; "Content-Type" = "application/xml; charset=utf-8"; Date = "Thu, 17 Oct 2013 19:45:07 GMT"; "Persistent-Auth" = true; Server = "Microsoft-IIS/8.0"; "X-AspNet-Version" = "4.0.30319"; "X-Powered-By" = "ASP.NET"; } }}
預先感謝您!
您是否嘗試過將ParameterEncoding設置爲'AFJSONParameterEncoding'?我不確定這是否會幫助你。我的一個應用程序下載PDF文件,但我最終使用NSURLConnection,而不是AFNetworking下載PDF文件。我發現它效果更好。如果您希望我可以與您分享該代碼。 – RyanG
@RyanG是的,我得到了'ExtendedException「,」message「:」這個文件無效。「 – Alan
我剛剛更新了我的評論 – RyanG