2015-09-25 118 views
0

我使用AFNetworking 2.0AFNetworking 2.0總是返回403

self.managerMasterFetchDeletes = [AFHTTPRequestOperationManager manager]; 
     self.managerMasterFetchDeletes.requestSerializer.timeoutInterval = 4.0f; 
     self.managerMasterFetchDeletes.requestSerializer = [AFJSONRequestSerializer serializer]; 
     self.managerMasterFetchDeletes.responseSerializer = [AFJSONResponseSerializer serializer]; 

NSURL *URL = [NSURL URLWithString:rest]; 
    NSURLRequest *request = [NSURLRequest requestWithURL:URL]; 
    AFHTTPRequestOperation *op = [[AFHTTPRequestOperation alloc] initWithRequest:request]; 
    op.responseSerializer = [AFJSONResponseSerializer serializer]; 

    [op setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) { 
     if (operation.response.statusCode==200){ 

      NSDictionary *dictionary = (NSDictionary*)responseObject; 
      NSMutableDictionary *responseData = [NSMutableDictionary new]; 
      if ([dictionary objectForKey:@"responseData"]) { 
       responseData = [[dictionary objectForKey:@"responseData"] mutableCopy]; 
      } 
      [responseData setValue:entity forKey:@"entityName"]; 
      [responseData setValue:[NSNumber numberWithInt:timeStamp] forKey:@"lastSync"]; 
      [[NSNotificationCenter defaultCenter] 
      postNotificationName:notification 
      object:nil userInfo:responseData]; 

     } 
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) { 
     NSLog(@"Error: %@", error); 
    }]; 

NSArray *operations = [AFURLConnectionOperation batchOfRequestOperations:[arrayOperations copy] progressBlock:^(NSUInteger numberOfFinishedOperations, NSUInteger totalNumberOfOperations) { 
     NSLog(@"%lu of %lu fetch delete complete", numberOfFinishedOperations, totalNumberOfOperations); 
    } completionBlock:^(NSArray *operations) { 
     NSLog(@"All operations in batch complete"); 
     [[NSNotificationCenter defaultCenter] 
     postNotificationName:@"MasterFecthDeleteEnd" 
     object:nil 
     userInfo:nil]; 
    }]; 

    [self.managerMasterFetchDeletes.operationQueue addOperations:operations waitUntilFinished:NO]; 

做了JSON的GET請求,但它總是與此錯誤返回403:不可接受的內容類型:text/html的

怪異事情是,我與可可GraphicalHttpClient做了同樣的要求,它的確定和返回JSON響應與此:

內容類型:應用程序/ JSON的;字符集= UTF-8 日期:星期五,2015年9月25日15:28 :27 GMT 傳輸編碼:身份 X-Powered-By:Servlet/2.5 JSP/2.1

非常感謝。

+1

第三方服務器發送給您的禁止錯誤,而不是'AFNetworking'框架......你可能需要從系統管理員爲你要訪問的內容請求許可。 – holex

+0

Add manager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@「text/html」] ;.希望它的工作,如果它不工作,你可以發佈你的代碼? –

+0

我試過,但沒有運氣。 – roof

回答

1

看起來你的服務器發送文本/ HTML ContentTypes。 要接受此類內容,請添加文字/ html acceptableContentTypes

AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager]; 
manager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/html"]; 
[manager GET:url parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) { 

} failure:^(AFHTTPRequestOperation *operation, NSError *error) { 

}]; 
1

這意味着您的服務器正在發送"text/html"而不是已支持的類型。我的解決方案是將"text/html"添加到AFURLResponseSerialization類中設置的可接受的內容類型中。只需搜索「acceptableContentTypes」並手動將@"text/html"添加到設置。

op.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/html"]; 

當然,理想的解決辦法是改變從服務器發送的類型,但是,你將不得不與服務器團隊交談。

在你的情況下,你可以這樣做。

NSURL *URL = [NSURL URLWithString:rest]; 
    NSURLRequest *request = [NSURLRequest requestWithURL:URL]; 
    AFHTTPRequestOperation *op = [[AFHTTPRequestOperation alloc] initWithRequest:request]; 
    op.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/html"]; 

    [op setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) { 
     if (operation.response.statusCode==200){ 

      NSDictionary *dictionary = (NSDictionary*)responseObject; 
      NSMutableDictionary *responseData = [NSMutableDictionary new]; 
      if ([dictionary objectForKey:@"responseData"]) { 
       responseData = [[dictionary objectForKey:@"responseData"] mutableCopy]; 
      } 
      [responseData setValue:entity forKey:@"entityName"]; 
      [responseData setValue:[NSNumber numberWithInt:timeStamp] forKey:@"lastSync"]; 
      [[NSNotificationCenter defaultCenter] 
      postNotificationName:notification 
      object:nil userInfo:responseData]; 

     } 
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) { 
     NSLog(@"Error: %@", error); 
    }]; 
+0

我試過了。注意,使用httpclient它可以正常工作並返回: – roof

+0

Content-Type:application/json; charset = UTF-8日期:2015年9月25日星期五15:28:27 GMT傳輸編碼:標識X-Powered-By:Servlet /2.5 JSP/2.1 – roof