0
我的应用正在与SharePoint站点通信,所以我使用身份验证质询来传递身份验证以使用SharePoint。身份验证challange iOS发出错误401
所有Web服务都得到正确的响应,但是当我试图下载图像,它会给我401(身份验证失败)错误约2至5倍,然后它会得到成功响应200
下面是代码:
-(void)getResponseArray:(NSString *)urlstring SoapMessage:(NSString *)soapMessage SoapAction:(NSString *)soapAction1
{
self.urlString = urlstring;
NSString *fullsoapMessage = [NSString stringWithFormat:@"<?xml version='1.0' encoding='utf-8'?><soap:Envelope xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:soap='http://schemas.xmlsoap.org/soap/envelope/'><soap:Body>%@</soap:Body></soap:Envelope>", soapMessage];
if(IS_DEBUG)
NSLog(@"The request: %@", fullsoapMessage);
NSURL *webserviceurl = [NSURL URLWithString:urlstring];
if (request == nil) {
request = [[NSMutableURLRequest alloc]initWithURL:webserviceurl];
}else{
[request setURL:webserviceurl];
}
NSString *msglength = [NSString stringWithFormat:@"%d", fullsoapMessage.length];
[request addValue:@"text/xml; charset=utf-8" forHTTPHeaderField:@"Content-Type"];
[request addValue:soapAction1 forHTTPHeaderField:@"SOAPAction"];
[request addValue:msglength forHTTPHeaderField:@"Content-Length"];
[request setHTTPMethod:@"POST"];
[request setHTTPBody:[fullsoapMessage dataUsingEncoding:NSUTF8StringEncoding]];
[request setTimeoutInterval:10000];
NSURLConnection *conn = [[NSURLConnection alloc]initWithRequest:request delegate:self];
if (conn) {
webData = [[NSMutableData alloc] init];
}else{
if(IS_DEBUG)
NSLog(@"Connection is NULL");
}
}
-(BOOL)connection:(NSURLConnection *)connection canAuthenticateAgainstProtectionSpace:(NSURLProtectionSpace *)protectionSpace {
return YES;
}
-(void)connection:(NSURLConnection *)connection didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge {
if ([challenge previousFailureCount] == 0)
{
NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
NSString *username = [userDefaults valueForKey:KEY_ADMIN_USERNAME];
NSString *password = [userDefaults valueForKey:KEY_ADMIN_PASSWORD];
NSURLCredential *newCredential;
newCredential = [NSURLCredential credentialWithUser:username password:password persistence:NSURLCredentialPersistenceNone];
[[challenge sender] useCredential:newCredential forAuthenticationChallenge:challenge];
} else {
[[challenge sender] cancelAuthenticationChallenge:challenge];
}
}
任何一个请建议我一些解决方案:
- 如何使用NSURLAuthenticationChallenge,以便SharePoint站点在首次请求请求时接受凭据,并且由于身份验证失败而得到响应200而不发生请求失败。
在此先感谢。
发布您的代码为NSMutableURLRequest。 –
@βhargavḯ:该问题与NSURLCONNECTION有关。 – Nikh1414
我编辑了问题 – Nikh1414