【问题标题】:Xcode https call to Restful APIXcode https 调用 Restful API
【发布时间】:2017-08-10 08:29:56
【问题描述】:

我在调用我们的 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);
                 }
             }
   ];

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

【问题讨论】:

  • 您应该使用 URLSession,因为 NSURLConnection 在 iOS 10 中已被弃用
  • 它能解决我的问题吗?还是只是一个建议?
  • 这是一个建议,你可以试试
  • 你能显示日志的结果吗?
  • 感谢富维!您建议的解决方案有效。

标签: ios iphone xcode ios8


【解决方案1】:

我运行了你的代码和日志,我得到的是

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];

【讨论】:

  • 是的,我确信我的代码不会到达服务器。我更改了 URL 以及 object[] 中的实际值。但是,是的,我现在正在按照您和 Phu Duy 的建议尝试使用 NSURLSession
  • 我要告诉你的是,上面是 NSUrlSession 的代码。我觉得你的链接有问题。如果你能给我一些其他的,我可以试试。
猜你喜欢
  • 2019-08-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-11-22
  • 2013-05-26
相关资源
最近更新 更多