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

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

问题描述

我需要使用Basic 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 设置为同步才能执行基本身份验证?根据帖子上的答案,您似乎无法进行基本身份验证如果您选择异步路由。

  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请求上说明Basic Auth的任何示例代码无需挑战回应? 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天全站免登陆