Xcode https调用Restful API

问题描述:

我在调用RESTful API时遇到问题。该请求是从API获取令牌;我需要该令牌来使用它来进一步调用该API。Xcode https调用Restful API

问题是,每当我打这个电话,我得到HTTP 403状态码,我不知道为什么。我正在从Android进行相同的调用,但它们工作正常,没有任何问题。

其实我没有任何经验可以使用xCode;我只是对其他开发人员编写的现有代码进行一些更改。我不知道我是否做错了什么或什么。

id keys[] = {@"grant_type", @"client_id", @"client_secret", @"username", @"password"}; 
    id objects[] = {@"password", @"AppClientID", @"AppClientSecret", @"usernamehere", @"userpasswordhere"}; 

    NSUInteger count = sizeof(objects)/sizeof(id); 
    NSDictionary *d = [NSDictionary dictionaryWithObjects:objects forKeys:keys count:count]; 

    NSData *jsonData = [NSJSONSerialization dataWithJSONObject:d options:NSJSONWritingPrettyPrinted error:nil]; 

    NSURL *url = [NSURL URLWithString:@"https://oauth-test.websitename.com/token"]; 

    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:60]; 

    [request setHTTPMethod:@"POST"]; 

    [request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"]; 
    [request setValue:@"application/json" forHTTPHeaderField:@"Accept"]; 

    [request setValue:@"Basic" forHTTPHeaderField:@"Authorization"]; 
    [request setHTTPBody:jsonData]; 

    [NSURLConnection sendAsynchronousRequest:request 
      queue:[NSOperationQueue mainQueue] 
      completionHandler:^(NSURLResponse *response, 
      NSData *data, NSError *connectionError) { 

       if(data.length > 0 && connectionError == nil) { 
        NSLog(@"Response --> %@ ", response); 
       } 
      } 
    ]; 

有人可以试图帮助我找出究竟是哪里出了问题吗? 在此先感谢。

+0

您应该使用URLSession因为NSURLConnection的是iOS的10 –

+0

不赞成将它解决我的问题?或者这只是一个建议? – user2908751

+0

这是一个建议,你可以试试 –

我跑你的代码和日志,我得到的是

Error Domain=NSURLErrorDomain Code=-1003 "A server with the specified 
hostname could not be found." UserInfo= 
{NSUnderlyingError=0x608000059aa0 {Error Domain=kCFErrorDomainCFNetwork 
Code=-1003 "A server with the specified hostname could not be found." 
UserInfo={NSErrorFailingURLStringKey=http://oauth- 
test.websitename.com/token, NSErrorFailingURLKey=http://oauth- 
test.websitename.com/token, _kCFStreamErrorCodeKey=8, 
_kCFStreamErrorDomainKey=12, NSLocalizedDescription=A server with the 
specified hostname could not be found.}}, 
NSErrorFailingURLStringKey=http://oauth-test.websitename.com/token, 
NSErrorFailingURLKey=http://oauth-test.websitename.com/token, 
_kCFStreamErrorDomainKey=12, _kCFStreamErrorCodeKey=8, 
NSLocalizedDescription=A server with the specified hostname could not 
be found.} 

我NSUrlSession尝试过也,但它是给同样的问题。 我认为你的服务器没有配置

下面是一个URL会话代码

id keys[] = {@"grant_type", @"client_id", @"client_secret", 
@"username", @"password"}; id objects[] = {@"password", @"AppClientID", @"AppClientSecret", @"usernamehere", @"userpasswordhere"}; 

NSUInteger count = sizeof(objects)/sizeof(id); NSDictionary *d = [NSDictionary dictionaryWithObjects:objects forKeys:keys count:count]; 

NSData *jsonData = [NSJSONSerialization dataWithJSONObject:d options:NSJSONWritingPrettyPrinted error:nil]; 

NSURL *url = [NSURL URLWithString:@"http://oauth-test.websitename.com/token"]; 

NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:60]; 

[request setHTTPMethod:@"POST"]; 

[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"]; 
[request setValue:@"application/json" forHTTPHeaderField:@"Accept"]; 

[request setValue:@"Basic" forHTTPHeaderField:@"Authorization"]; [request setHTTPBody:jsonData]; 

NSURLSession * session = [NSURLSession sessionWithConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]]; 
NSURLSessionDownloadTask *dataTask = [session downloadTaskWithRequest:request completionHandler:^(NSURL * _Nullable location, NSURLResponse * _Nullable response, NSError * _Nullable error) { 
    NSLog(@"%@", response); 

}]; 

[dataTask resume]; 
+0

是的,我相信我的代码不会到达服务器。我更改了URL以及对象[]中的实际值。但是,是的,我现在正按照你和Phu Duy的建议尝试使用NSURLSession – user2908751

+0

我告诉你的是,上面是NSUrlSession的代码。我认为你的链接存在问题。如果你可以给我一些其他像我可以尝试一下。 –