如何连接到需要用户名和密码的网站

我有一个基本的问题,我希望有一个简单的答案,我错过了。 基本前提是我要连接到一个网站并下载一些JSON数据。 我正在使用由Stig Brautaset提供的框架,该教程工作得很好。

我的问题是,我连接的网站是在格式和用户名和密码是固定的我的应用程序,所以用户将永远不会进入他们。

curl -u username:password http://website.com/page 

如何将url和密码传递给NSURLConnection

它不一定是一个NSURLConnection ,它似乎是最好的select。

我已经看了AdvancedUrlConnections样本,但它似乎过于复杂,是相当古老的。 在网上search并没有好多less。 我希望你们中的一个可以说只是设置这两个属性,其次是适当的侮辱,当然… … –

一个NSURLConnection将工作得很好。 正如在另一个答案中指出的,实现didReceiveAuthenticationChallengecallback是很容易的。

 - (void) someMethod { NSURLRequest* request = [[NSURLRequest alloc] initWithURL:[NSURL urlWithString:@"someURL"] NSURLConnection* connection = [[NSURLConnection alloc] initWithRequest:request delegate:self]; [connection release]; [request release]; } 

这是非常简单的。 它在NSURLConnection的所有魔术发生的委托方法中:

这是您处理证书挑战的地方。 我已经硬编码了一个假的用户名和密码来演示它是如何工作的。 我个人有一个单独的委托对象处理挑战。 请记住,连接将处于空闲状态,直到您响应或直到连接超时。

 - (void) connection:(NSURLConnection *)connection didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge { // Make sure to use the appropriate authentication method for the server to // which you are connecting. if ([[challenge protectionSpace] authenticationMethod] == NSURLAuthenticationMethodBasicAuth) { // This is very, very important to check. Depending on how your // security policies are setup, you could lock your user out of his // or her account by trying to use the wrong credentials too many // times in a row. if ([challenge previousFailureCount] > 0) { [[challenge sender] cancelAuthenticationChallenge:challenge]; UIAlertView* alert = [[UIAlertView alloc] initWithTitle:@"Invalid Credentials" message:@"The credentials are invalid." delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil]; [alert show]; [alert release]; } else { [challenge useCredential:[NSURLCredential credentialWithUser:@"someUser" password:@"somePassword" persistence:NSURLCredentialPersistenceForSession forAuthenticationChallenge:challenge]]; } } else { // Do whatever you want here, for educational purposes, // I'm just going to cancel the challenge [[challenge sender] cancelAuthenticationChallenge:challenge]; } } 

您还需要为NSURLConnection实现这些其他方法:

 // So you know when it's done downloading - (void) connectionDidFinishLoading:(NSURLConnection *)connection; // In case of failure - (void) connection:(NSURLConnection *)connection didFailWithError:(NSError *)error; // Gather the downloaded file data - (void) connection:(NSURLConnection *)connection didReceiveData:(NSData *)data; 

有几种可能性。

如果使用asynchronousNSURLConnection,则将在连接的委托上调用方法-connection:didReceiveAuthenticationChallenge: 这个方法的文档解释了如何处理它。 这是AdvancedUrlConnections使用的,是的,有点混乱。

一个更简单的方法是在创build连接时创build一个NSMutableURLRequest (而不是一个不可变的NSURLRequest )。 这使您可以添加任意的HTTP标头。 构build一个基本的auth头文件很简单 – 就像

 NSMutableURLRequest *request = .... // details omitted NSString *username = // .... NSString *password = // .... NSData *authRawData = [[NSString stringWithFormat:@"%@:%@", username, password] dataUsingEncoding:NSUTF8StringEncoding]; NSString *authEncoded = [authRawData asBase64EncodedString]; [request setValue:[NSString stringWithFormat:@"Basic %@", authEncoded] forHTTPHeaderField:@"Authorization"]; 

以上使用了-asBase64EncodedString包含的-asBase64EncodedString方法