Tôi đang làm một dịch vụ trong công ty khách hàng của mình. Và tôi cố gắng lấy một số thông tin từ máy chủ của họ bằng AFNetWorking (Khách hàng của chúng tôi khuyến khích sử dụng AFNetWorking) Tôi đã làm một số mẫu bằng cách sử dụng AFNetWorking và nó hoạt động. Nhưng khi tôi sử dụng một trong các URL của khách hàng để có được dữ liệu JSON, nó thất bại và đây là mô tả lỗi:Mã lỗi -1011 khi tôi sử dụng 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}
tôi cố gắng tìm ra một số giải pháp, nhưng tôi không thể sửa chữa được nêu ra. Có mã của tôi:
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];
Cảm ơn bạn đã đọc và mọi trợ giúp hoặc trả lời sẽ được đánh giá cao.
EDIT: Như DarkDust đã nói: máy chủ từ chối quyền truy cập của tôi. Nhưng tôi có thể lấy dữ liệu từ máy chủ bằng cách kết nối cơ bản: Đây là mã để có được:
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]];
}
Tôi tự hỏi tại sao rssConnection có thể nhận được JSON văn bản và AFHTTPClient có thể không?
Bạn có thực sự nghĩ rằng bạn nên đăng các chi tiết nội bộ/riêng tư của khách hàng mà không bị quấy rầy? – DarkDust
Đây chỉ là một trang thử nghiệm, không phải là trang riêng. Sẽ có nhiều thay đổi hơn trước khi tôi có thể hoàn thành. Và trong tương lai, sẽ cần có khóa API để truy cập, và tất nhiên, tôi giấu nó – magichero
Tuy nhiên, mọi người bây giờ có thể thấy khách hàng của bạn là ai. Khách hàng của bạn có thể không hài lòng về điều đó. – DarkDust