我正在使用同步请求并传递凭据,但我收到了身份验证错误。以下是我的服务器请求和响应代码。
要求: -
NSURLCredential *userCredentials = [NSURLCredential credentialWithUser:@"username"
password:@"paswd"
persistence:NSURLCredentialPersistenceForSession];
NSURLProtectionSpace *space = [[NSURLProtectionSpace alloc] initWithHost:@"http://webaddress.inc.com"
port:80
protocol:@"http"
realm:@"webaddress.inc.com"
authenticationMethod:nil];
[[NSURLCredentialStorage sharedCredentialStorage] setCredential:userCredentials
forProtectionSpace:space];
NSMutableURLRequest *urlRequest = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"http://webaddress.inc.com"]
cachePolicy:NSURLCacheStorageNotAllowed
timeoutInterval:30];
NSURLResponse *response;
NSError *error;
NSData *returnData = [NSURLConnection sendSynchronousRequest:urlRequest
returningResponse:&response
error:&error];
///////////回复
401需要授权
注意:如果我使用异步请求发送请求,那么它就像在这种情况下一样正常 didReceiveAuthenticationChallenge 在被要求时为他们提供所需的凭据。
我确信我的代码中可能缺少某些内容。
等待您的宝贵意见。