0

我正在尝试从 googletrends 下载文件。为此,我必须登录。我正在使用 NSURLConnection 所以我想我必须使用

- (void)connection:(NSURLConnection *)connection willSendRequestForAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge

我必须设置什么参数?即我如何定义用户名和密码?

4

1 回答 1

2

使用NSURLCredential传递用户名和密码:

- (void)connection:(NSURLConnection *)connection willSendRequestForAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge
{
    NSURLCredential *credential = [NSURLCredential credentialWithUser:userName password:userPassword persistence:NSURLCredentialPersistenceForSession];

    [[challenge sender] useCredential:credential forAuthenticationChallenge:challenge];
}

请查看本教程:处理 URL 身份验证挑战

于 2013-02-02T11:51:38.120 回答