iOS 中的 NSURLConnection 和基本 HTTP 身份验证 [英] NSURLConnection and Basic HTTP Authentication in iOS

查看:21
本文介绍了iOS 中的 NSURLConnection 和基本 HTTP 身份验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要使用基本Authentication 调用初始GET HTTP 请求.这将是第一次将请求发送到服务器并且我已经拥有 用户名 &密码,所以不需要来自服务器的挑战来授权.

I need to invoke an initial GET HTTP request with Basic Authentication. This would be the first time the request is sent to the server and I already have the username & password so there's no need for a challenge from the server for authorization.

第一个问题:

  1. NSURLConnection 是否必须设置为同步才能进行基本身份验证?根据这个post的回答,看来你不能做基本身份验证如果您选择异步路由.

  1. Does NSURLConnection have to be set as synchronous to do Basic Auth? According to the answer on this post, it seems that you can't do Basic Auth if you opt for the async route.

有谁知道一些示例代码可以在不需要质询响应的情况下说明 GET 请求 上的基本身份验证?Apple 的文档 显示了一个示例,但仅在服务器向客户端发出质询请求后.

Anyone know of any some sample code that illustrates Basic Auth on a GET request without the need for a challenge response? Apple's documentation shows an example but only after the server has issued the challenge request to the client.

我对 SDK 的网络部分有点陌生,我不确定应该使用哪些其他类来使其工作.(我看到了 NSURLCredential 类,但它似乎仅在客户端向服务器请求授权资源后才与 NSURLAuthenticationChallenge 一起使用).

I'm kind of new the networking portion of the SDK and I'm not sure which of the other classes I should use to get this working. (I see the NSURLCredential class but it seems that it is used only with NSURLAuthenticationChallenge after the client has requested for an authorized resource from the server).

推荐答案

我正在使用与 MGTwitterEngine 并在 NSMutableURLRequest (theRequest) 中设置授权,如下所示:

I'm using an asynchronous connection with MGTwitterEngine and it sets the authorization in the NSMutableURLRequest (theRequest) like so:

NSString *authStr = [NSString stringWithFormat:@"%@:%@", [self username], [self password]];
NSData *authData = [authStr dataUsingEncoding:NSASCIIStringEncoding];
NSString *authValue = [NSString stringWithFormat:@"Basic %@", [authData base64EncodingWithLineLength:80]];
[theRequest setValue:authValue forHTTPHeaderField:@"Authorization"];

我不认为这种方法需要通过挑战循环,但我可能是错的

I don't believe this method requires going through the challenge loop but I could be wrong

这篇关于iOS 中的 NSURLConnection 和基本 HTTP 身份验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆