gpt4 book ai didi

xcode - NSURLConnection 发送异步请求 :queue:completionHandler: doesn't call delegate method - didReceiveAuthenticationChallenge

转载 作者:行者123 更新时间:2023-12-04 19:40:55 25 4
gpt4 key购买 nike

我有一个受摘要保护的 REST API。我想下载我的 JSON 响应,但首先我必须针对其余 api 进行身份验证。
我正在使用 sendAsynchronousRequest:queue:completionHandler: 处理我的请求。但我不知道如何处理摘要身份验证。我认为使用 NSURLConnectionDelegate 的委托(delegate)方法 didReceiveAuthenticationChallenge 这应该是可能的?我在 .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"); }

最佳答案

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

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

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

您将需要实现进一步的委托(delegate)方法才能接收响应。

请注意 connection:didReceiveAuthenticationChallenge:已弃用,取而代之的是其他委托(delegate)方法(参见 page )。

关于xcode - NSURLConnection 发送异步请求 :queue:completionHandler: doesn't call delegate method - didReceiveAuthenticationChallenge,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10207596/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com