Xcode https调用Restful API

时间:2017-08-10 08:29:56

标签: ios iphone xcode ios8

我在调用RESTful API时遇到问题。请求是从API获取令牌;我需要该令牌来使用它来进一步调用该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);
                 }
             }
   ];

有人可以帮我弄清楚究竟出了什么问题吗? 提前谢谢。

1 个答案:

答案 0 :(得分: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];