gpt4 book ai didi

web-services - iOS 4.3 - 尝试网络服务连接时网络连接丢失?

转载 作者:可可西里 更新时间:2023-11-01 16:31:15 25 4
gpt4 key购买 nike

我正在编写一些代码以连接到基于安全 .NET 的 Web 服务。我尝试使用手动创建的 SOAP 消息、HTTP POST 和 HTTP GET 进行连接。在每次尝试中,我都在 NSURLConnection 上使用 NSMutableURLRequest。我已经通过以下方式实现了 didReceiveAuthenticationChallenge...

- (void)connection:(NSURLConnection *)connection didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge
{
NSLog(@"Authentication challenge");

if ([challenge previousFailureCount] == 0)
{
NSLog(@"authentication failed");

NSURLCredential *credential ;

credential = [NSURLCredential credentialWithUser:@"username" password:@"password" persistence:NSURLCredentialPersistenceForSession];

NSLog(@"credential created");

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

NSLog(@"credential sent");
}
else
{
NSLog(@"Credentials are wrong!");

[[challenge sender] cancelAuthenticationChallenge:challenge];
}
}

出现所有日志消息。写入“凭据已发送”约 30 秒后,出现以下错误。

Connection failed: Error Domain=NSURLErrorDomain Code=-1005 "The network connection was lost."

我在这里和其他地方找到了大量关于处理身份验证质询的示例。我所做的一切似乎都不寻常。我还实现了以下以防万一,但结果是一样的。

- (BOOL)connection:(NSURLConnection *)connection canAuthenticateAgainstProtectionSpace:(NSURLProtectionSpace *)protectionSpace 
{
NSLog(@"canAuthenticateAgainstProtectionSpace");

//return YES to say that we have the necessary credentials to access the requested resource
return YES;
}

我为响应挑战而发送的用户名/密码存在。我已通过在浏览器中输入服务 URL 并在身份验证对话框中输入用户名/密码进行验证。通过浏览器调用服务时返回 XML。

这几天我一直在用头撞墙。任何指针将不胜感激。

谢谢

肖恩

最佳答案

我刚刚发现我的错误。 URL 没有被正确编码,但直到我深入了解 NSUrl URLWithString: 后我才意识到这一点。我添加了以下代码,一切正常。

NSString *myURL = [NSString stringWithFormat:@"http://server/path/to/service.asmx/Method?param=%@", param.text];

NSString *fixedURL = [myURL stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

NSURL *URL = [NSURL URLWithString:fixedURL];

NSMutableURLRequest *req = [NSMutableURLRequest requestWithURL:URL];

关于web-services - iOS 4.3 - 尝试网络服务连接时网络连接丢失?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5437614/

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