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