JSON GET方法请求

时间:2011-02-11 12:10:12

标签: json iphone

我开发了一个需要通过HTTP身份验证请求标头发送用户名和密码的iphone应用程序。任何人都可以指导我或发送该功能的源代码。

1 个答案:

答案 0 :(得分:0)

您需要实现NSURLConnection:didReceiveAuthenticationChallenge:方法。

- (void)request:(NSURLConnection *)connection didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge { 
    if ([challenge previousFailureCount] == 0) { 
        NSURLCredential *newCredential = [NSURLCredential
                                              credentialWithUser:@"username"
                                              password:@"password" 
                                              persistence:NSURLCredentialPersistenceNone]; 
        [[challenge sender] useCredential:newCredential 
               forAuthenticationChallenge:challenge]; 
    } 
    else { 
        [[challenge sender] cancelAuthenticationChallenge:challenge];
    } 
}

修改:或者您只需以http://username:password@www.example.com/

格式在网址中发送凭据即可