加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

xcode – NSURLConnection sendAsynchronousRequest:queue:com

发布时间:2020-12-14 17:30:22 所属栏目:百科 来源:网络整理
导读:我有一个REST API,它由摘要保护.我想下载我的 JSON响应,但首先我要对其余的api进行身份验证. 我正在使用sendAsynchronousRequest执行我的请求:queue:completionHandler:.但我不知道如何处理摘要身份验证.我想用委托方法didReceiveAuthenticationChallenge
我有一个REST API,它由摘要保护.我想下载我的 JSON响应,但首先我要对其余的api进行身份验证.
我正在使用sendAsynchronousRequest执行我的请求:queue:completionHandler:.但我不知道如何处理摘要身份验证.我想用委托方法didReceiveAuthenticationChallenge的NSURLConnectionDelegate这应该可行吗?我在.h文件中声明了NSURLConnectionDelegate,并在实现中添加了该方法.但没有任何反应.有关如何使用“sendAsynchronousRequest:queue:completionHandler:”处理此问题的任何建议?

NSURL *url = [NSURL URLWithString:@"http://restapi/"];

NSURLRequest *urlRequest = [NSURLRequest requestWithURL:url];
NSOperationQueue *queue = [[NSOperationQueue alloc] init];

[NSURLConnection sendAsynchronousRequest:urlRequest queue:queue completionHandler:^(NSURLResponse *response,NSData *data,NSError *error)
 {
     if ([data length] > 0 && error == nil)
         [self receivedData:data];
     else
         NSLog(@"error");
 }];

- (void)connection:(NSURLConnection *)connection didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge {
NSLog(@"did get auth challenge"); }

解决方法

连接:didReceiveAuthenticationChallenge:仅当您将实例指定为连接的委托时才会被调用.为此,您需要使用不同的方法来启动请求,例如:

NSURL *url = [NSURL URLWithString:@"http://restapi/"];

NSURLRequest *urlRequest = [NSURLRequest requestWithURL:url];
NSURLConnection *connection = [NSURLConnection connectionWithRequest:urlRequest delegate:self]

您需要实现进一步的委托方法才能接收响应.

请注意,不建议使用connection:didReceiveAuthenticationChallenge:以支持其他委托方法(请参阅此page).

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读