【问题标题】:ios sendAsynchronousRequest redirect basic authios sendAsynchronousRequest 重定向基本身份验证
【发布时间】:2012-11-09 19:40:19
【问题描述】:

是否可以使用NSURLConnectionsendAsynchronousRequest 知道请求的 url 将具有重定向并且所有请求也必须使用基本身份验证?

我收到以下错误:Error Domain=NSURLErrorDomain Code=-1012 "无法完成操作。(NSURLErrorDomain 错误-1012。)

我确实使用NSURLConnectionDelegate 编写了一些代码并修改了重定向请求以在标头中添加基本身份验证,并且有效。

所以我猜这与第二个请求没有设置身份验证有关。与委托相同,如果我没有在重定向请求上设置基本身份验证,则 HTTP 401 未授权

失败

【问题讨论】:

  • 那个答案不是我想要的。我知道如何使用 sendasync,但我的问题在于重定向和基本身份验证。
  • @pdiddy 你解决了这个问题吗?
  • 不,我们决定不进行重定向,所以这不再是问题

标签: ios authentication redirect nsurlconnection


【解决方案1】:

我相信我目前正在这样做,这是我的代码,诀窍是使用“基本 base64encodedmethod(username:password)”设置“授权”的标题

//SETUP URL
NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"https://YOURURLHERE"];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url
                                                       cachePolicy:NSURLRequestReloadIgnoringCacheData
                                                   timeoutInterval:90.0];

//Get the username & password - then encode the values with Base64 (i googled for a library)
 NSString *userAndPassword = [NSString stringWithFormat:@"%@:%@",userName,password];
    NSString *encodedString = [userAndPassword base64EncodedString];

//Set the Authorization header which will now let your service calls pass basic authentication
    [request addValue:[NSString stringWithFormat:@"Basic %@",encodedString] forHTTPHeaderField:@"Authorization"];

//Setup a queue and call the async request - remember to do items in your block on the 
//main thread if it's for things like showing/hiding items as other threads will not run that code when you're expecting it

NSOperationQueue *queue = [[NSOperationQueue alloc] init];
[NSURLConnection sendAsynchronousRequest:request queue:queue completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) {
//your code block here 
}];

我相信这回答了您的问题,现在我只需要了解如何删除该身份验证,以便在为安全的合法网站构建此身份验证时注销用户。任何帮助将不胜感激

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-12-31
    • 2021-06-20
    • 1970-01-01
    • 2013-10-29
    • 2013-06-17
    • 1970-01-01
    • 1970-01-01
    • 2013-04-11
    相关资源
    最近更新 更多