gpt4 book ai didi

iphone - NSURLErrorDomain 错误-1012

转载 作者:技术小花猫 更新时间:2023-10-29 11:04:48 27 4
gpt4 key购买 nike

我需要从受密码保护的 URL 解析 xml 文件,我尝试了以下操作

NSURLCredential *credential = [NSURLCredential credentialWithUser:@"admin"  password:@"123456" persistence:NSURLCredentialPersistenceForSession];
NSURLProtectionSpace *protectionSpace = [[NSURLProtectionSpace alloc]
initWithHost:@"xyz.com"
port:80
protocol:@"http"
realm:nil
authenticationMethod:NSURLAuthenticationMethodDefault];
[[NSURLCredentialStorage sharedCredentialStorage] setCredential:credential
forProtectionSpace:protectionSpace];
url = [NSURL URLWithString:urlString];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
[request setHTTPMethod:@"GET"];
[request setValue:@"application/xml" forHTTPHeaderField:@"Content-Type"];
connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
NSURLResponse *response;
NSError *error;
NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse: &response error: &error];
NSString *dataStr=[[NSString alloc]initWithData:returnData encoding:NSUTF8StringEncoding];
NSLog(@"data == %@\n error in connecting == %@",dataStr,error);

我得到了以下响应

data == <!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html><head>
<title>401 Authorization Required</title>
</head><body>
<h1>Authorization Required</h1>
<p>This server could not verify that you
are authorized to access the document
requested. Either you supplied the wrong
credentials (e.g., bad password), or your
browser doesn't understand how to supply
the credentials required.</p>
<p>Additionally, a 401 Authorization Required
error was encountered while trying to use an ErrorDocument to handle the request.</p>
</body></html>

连接错误 == Error Domain=NSURLErrorDomain Code=-1012 "The operation couldn't be completed. (NSURLErrorDomain error -1012.)"UserInfo=0x6e51b90 {NSErrorFailingURLKey= http://example.com/api/ID/password/xml/ ,

感谢任何帮助!

最佳答案

当使用 NSURLRequest 访问服务器时,NSURLConnection 的委托(delegate)方法提供了一种身份验证时得到通知的方法受到挑战

下面的示例将展示一种处理要求凭据的 URL 的方法。

- (void)CallPasswordProtectedWebService
{
NSURL *url = [NSURL URLWithString:@"your url"];
NSMutableURLRequest *urlRequest = [NSMutableURLRequest requestWithURL:url];
urlConnection = [[NSURLConnection alloc] initWithRequest:urlRequest delegate:self];
}

//when server asks the credentials this event will fire

- (void)connection:(NSURLConnection *)connection
didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge
{

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

//when connection succeeds this event fires

- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
NSLog(@"Successfuly connected ");

}

//when connection fails the below event fires

- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
NSLog(@"connection failed");

}

希望对你有帮助

关于iphone - NSURLErrorDomain 错误-1012,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11607214/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com