2011-11-03 95 views
8

我正在我們的客戶公司做一項服務。我試圖通過AFNetWorking從他們的服務器獲得一些信息(我們的客戶鼓勵使用AFNetWorking) 我做了一些使用AFNetWorking的示例,它是可行的。 但是當我使用我們的客戶的一個URL來獲取JSON數據,它失敗了,這是錯誤的描述:錯誤代碼-1011當我使用AFNetWorking

Error Domain=com.alamofire.networking.error Code=-1011 
"Expected status code <NSIndexSet: 0x7e274f0>[number of indexes: 100 (in 1 ranges), 
indexes: (200-299)], got 403" UserInfo=0x7b64040 {NSErrorFailingURLKey=<url_hidden_for_stackoverflow>, 
NSLocalizedDescription=Expected status code <NSIndexSet: 0x7e274f0>[number of indexes: 100 (in 1 ranges), indexes: (200-299)], got 403} 

我試圖找出一些解決方案,但我不能修復呢。有我的代碼:

NSURL *url = [NSURL URLWithString:@"http://example.com/"]; 
AFHTTPClient *httpClient = [[AFHTTPClient alloc] initWithBaseURL:url]; 
//[httpClient setDefaultHeader:@"Accept" value:@"text/json"]; 
//NSDictionary *params = [NSDictionary dictionaryWithObjectsAndKeys:CONST_KEY_REGISTER_UNIQUE_KEY, CONST_API_KEY_TEXT,nil]; 
NSMutableURLRequest *request = [httpClient requestWithMethod:@"POST" path:@"path/to/page.json" parameters:nil]; 
[httpClient release]; 
AFJSONRequestOperation *operation = [AFJSONRequestOperation JSONRequestOperationWithRequest:request success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) { 
    NSString *status = [JSON valueForKey:@"status"]; 
    if ([status isEqualToString:@"OK"]) { 
     NSString *uniqueId = [JSON valueForKey:@"uniqueId"]; 
     [UserSettings setWithKey:CONST_PROGRAM_UNIQUE_KEY value:uniqueId]; 
    } 
    //NSString *message = [json valueForKey:@"message"]; 
    //NSString *error = [json valueForKey:@"error"]; 
    [[LoadingView instance] performSelectorOnMainThread:@selector(removeLoadingView) withObject:nil waitUntilDone:YES]; 
} 
failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error) { 
    NSString *errorString = [error description]; 
    [[LoadingView instance] performSelectorOnMainThread:@selector(removeLoadingView) withObject:nil waitUntilDone:YES]; 
}]; 
NSOperationQueue *queue = [[[NSOperationQueue alloc] init] autorelease]; 
[queue addOperation:operation]; 

感謝您的閱讀,任何幫助或答覆將不勝感激。

編輯:正如DarkDust所說:服務器拒絕我的訪問。但我可以通過基本的連接從服務器獲取數據: 這裏是代碼獲取:

NSURL *url = [NSURL URLWithString:@"http://example.com/path/to/page.json"]; 
NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:url cachePolicy:NSURLRequestReloadIgnoringLocalAndRemoteCacheData timeoutInterval:CONST_CONNECTION_TIMEOUT]; 
rssConnection = [[NSURLConnection alloc] initWithRequest:theRequest delegate:self]; 
[self performSelectorOnMainThread:@selector(downloadStarted) withObject:nil waitUntilDone:NO]; 
if (rssConnection != nil) { 
    do { 
     [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate distantFuture]]; 
    } while (!done); 
} 

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data { 
    // I can get text here, but it is not JSON format 
NSString *content = [NSString stringWithUTF8String:[data bytes]]; 
} 

我不知道爲什麼rssConnection可以得到JSON文本和AFHTTPClient不能?

+1

您是否真的認爲在不混淆的情況下發布客戶的內部/隱私細節是一個好主意? – DarkDust

+0

這只是一個測試頁面,不是私人頁面。在我完成之前會有更多的變化。未來,需要API密鑰才能訪問,當然,我也會隱藏它 – magichero

+1

不過,現在每個人都可以看到你的客戶是誰。您的客戶可能不會很高興。 – DarkDust

回答

1

服務器響應HTTP錯誤代碼403意味着禁止。它拒絕你訪問。您需要找出原因,例如通過閱讀服務器日誌(如果可以的話)或要求服務器管理員來幫助您。它可能是需要提升/修改的服務器訪問限制。

編輯:HTTP POST是一種想要在服務器上保存某些內容的操作。雖然正常GET似乎根據您編輯的問題正常工作,現在禁止保存。首先要做的是檢查服務器配置。另外,如果你的URL指向一個腳本(JSP,ASP,無論什麼),這對你的情況來說是唯一有意義的,你需要檢查它以確定它拒絕訪問的原因(如果服務器配置沒有否認它,它必須是腳本)。

+0

您發佈的錯誤消息清楚地說明了'預期狀態碼...,得到403'。服務器拒絕你訪問。 – DarkDust

+0

我編輯我的帖子,因爲如果我添加評論,我無法清楚地告訴你我需要什麼。 – magichero

+0

@magichero:更新了我的答案。 – DarkDust

23

由於因爲通過谷歌搜索高結果引用...

對於其他那些正在尋求通過AFNetworking獲取可能的錯誤代碼,請參考蘋果文檔URL Loading System Error Codes,因爲這些是相同的。

NSURLErrorBadServerResponse = -1011

當URL加載系統從服務器接收錯誤的數據返回。 這相當於HTTP服務器發送的「500服務器錯誤」消息。

相關問題