2
我想獲得一個簡單的GET請求來爲我的api工作。但它似乎並沒有工作,我不知道爲什麼。使用NSURL不能正常工作的GET請求
我的代碼如下
編輯,還試圖NSURLCredential但是,這似乎並沒有工作,要麼
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
NSString *urlAsString = @"https://www.test.com/api/v1/user/logout/";
NSURL *url = [NSURL URLWithString:urlAsString];
NSMutableURLRequest *urlRequest = [NSMutableURLRequest requestWithURL:url]; [urlRequest setTimeoutInterval:30.0f];
[urlRequest setHTTPMethod:@"GET"];
NSOperationQueue *queue = [[NSOperationQueue alloc] init];
[NSURLConnection sendAsynchronousRequest:urlRequest queue:queue completionHandler:^(NSURLResponse *response,NSData *data, NSError *error) {
if ([data length] >0 && error == nil){
NSString *html = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
NSLog(@"HTML = %@", html); }
else if ([data length] == 0 && error == nil){
NSLog(@"Nothing was downloaded."); }
else if (error != nil){
NSLog(@"Error happened = %@", error); }
}];
}
- (BOOL)connection:(NSURLConnection *)connection canAuthenticateAgainstProtectionSpace:(NSURLProtectionSpace *)protectionSpace {
return [protectionSpace.authenticationMethod isEqualToString:NSURLAuthenticationMethodServerTrust];
}
- (void)connection:(NSURLConnection *)connection didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge {
[challenge.sender useCredential:[NSURLCredential credentialForTrust:challenge.protectionSpace.serverTrust] forAuthenticationChallenge:challenge];
[challenge.sender continueWithoutCredentialForAuthenticationChallenge:challenge];
}
我的API吐出JSON。所以,我應該得到一個JSON對象回來,說
success: False
reason: 'Already Logged out'
但相反,它給了我下面的錯誤
2013-03-07 16:24:44.038 test[8957:1d03] Error happened = Error Domain=NSURLErrorDomain Code=-1012 "The operation couldn’t be completed. (NSURLErrorDomain error -1012.)" UserInfo=0x715db20 {NSErrorFailingURLKey=https://www.test.com/api/v1/user/logout/, NSErrorFailingURLStringKey=https://www.test.com/api/v1/user/logout/, NSUnderlyingError=0x7181cb0 "The operation couldn’t be completed. (kCFErrorDomainCFNetwork error -1012.)"}
方法2
一些研製後,我發現發送的另一個礙事請求,並且此方法似乎正常工作
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
dispatch_async(queue, ^{
NSError *error = nil;
NSURL *url = [NSURL URLWithString:@"https://www.test.com/api/v1/user/logout/"];
NSString *json = [NSString stringWithContentsOfURL:url
encoding:NSASCIIStringEncoding
error:&error];
if(!error) {
NSData *jsonData = [json dataUsingEncoding:NSASCIIStringEncoding];
NSDictionary *jsonDict = [NSJSONSerialization JSONObjectWithData:jsonData
options:kNilOptions
error:&error];
BOOL success = [[jsonDict objectForKey:@"success"] boolValue];
if (!success) {
NSString *reason = [jsonDict objectForKey:@"reason"];
NSLog(@"Cannot log out, as user %@", reason);
}
//NSLog(@"JSON: %@", jsonDict);
}else{
NSLog(@"\nJSON: %@ \n Error: %@", json, error);
}
});
'ErrorCode' 1012對應於'NSURLErrorUserCancelledAuthentication',您將必須實現'NSURLConnectionDelegate'的方法'connection:willSendRequestForAuthenticationChallenge:',請參閱[documentation](https://developer.apple.com/library/) mac/documentation/Foundation/Reference/NSURLConnectionDelegate_Protocol/Reference/Reference.html#// apple_ref/occ/intfm/NSURLConnectionDelegate/connection:willSendRequestForAuthenticationChallenge :) – tkanzakic 2013-03-07 20:28:05
@tkanzakic謝謝我會研究它。也是我的方法糾正JSON取回? – Jonathan 2013-03-07 20:45:28
是的,它的問題*是你正在使用安全協議,你將不得不管理證書驗證,看看[這個其他問題](http://stackoverflow.com/questions/10722272/) ios5-willsendrequestforauthenticationchallenge-method-is-running-recursive),你也可以在SO – tkanzakic 2013-03-07 20:57:45