【问题标题】:NSURLConnection Delegate Methods won't be called...can't figure it out and read a lot of postsNSURLConnection Delegate 方法不会被调用……想不通,看了很多帖子
【发布时间】:2010-11-23 18:21:25
【问题描述】:

我有一个名为BackendConnector 的类,它使用NSURLConnection 调用SoapWebservice,即https。我发现了很多帖子并尝试实现有关身份验证的委托方法,但它们不会被调用,并且在谷歌 6 小时后我没有得到我做错了什么。有人可以给我一个提示,为什么这两个委托方法不会被调用?我在每个中设置了一个断点,在模拟器中使用 XCode 启动了我的应用程序,但仍然出现错误并且断点没有被命中。

BackendConnector.m

#import "BackendConnector.h"

@implementation BackendConnector

@synthesize dataPoint, chartDataPoints;

- (NSMutableArray *)GetWebserviceData
{
    NSMutableData *myMutableData;
    chartDataPoints = [[NSMutableArray alloc] init];

    [self createWebserviceAndGetResponse: &myMutableData];

    if (myMutableData != NULL)
    {
        NSString *theXml = [[NSString alloc]initWithBytes:[myMutableData mutableBytes] length:[myMutableData length] encoding:NSUTF8StringEncoding];

        NSLog(@"doc = %@", theXml);

        arrayCount = 0;
        [self parseResponse: myMutableData];
    }
    else 
    {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error" 
                                                        message:@"There is no Data returned from Webservice!"
                                                       delegate:nil 
                                              cancelButtonTitle:@"OK" 
                                              otherButtonTitles: nil];
        [alert show];
        [alert release];
}

    return NULL;
    //return chartDataPoints;
}

- (void) createWebserviceAndGetResponse: (NSMutableData **) myMutableData_p  
{
    NSMutableString *sRequest = [[NSMutableString alloc]init];

    [sRequest appendString:@"<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:ser=\"http://service.ReportWebservice.xyz.com/\">"];
    [sRequest appendString:@"<soapenv:Header/>"];
    [sRequest appendString:@"<soapenv:Body>"];
    [sRequest appendString:@"<ser:getReport>"];
    [sRequest appendString:@"<arg0>User/arg0>"];
    [sRequest appendString:@"<arg1>password</arg1>"];
    [sRequest appendString:@"</ser:getReport>"];
    [sRequest appendString:@"</soapenv:Body>"];
    [sRequest appendString:@"</soapenv:Envelope>"];

    NSURL *myWebserverURL = [NSURL URLWithString:@"https://xyz.com/services/report"];

    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:myWebserverURL]; 
    NSLog(@"request: %@", request);

    [request addValue:@"text/xml; charset=utf-8" forHTTPHeaderField:@"Content-Type"];
    [request addValue:@"" forHTTPHeaderField:@"SOAPAction"];

    NSString *contentLengthStr = [NSString stringWithFormat:@"%d", [sRequest length]];

    [request addValue:contentLengthStr forHTTPHeaderField:@"Content-Length"];
    [request setHTTPMethod:@"POST"];
    [request setHTTPBody:[sRequest dataUsingEncoding:NSUTF8StringEncoding]];

    NSURLConnection *conn = [[NSURLConnection alloc] initWithRequest:request delegate:self];

    if(conn)
    {
        *myMutableData_p=[[NSMutableData data] retain];
    }

    [NSURLConnection connectionWithRequest:request delegate:self];

    NSError *WSerror;
    NSURLResponse *WSresponse;

    *myMutableData_p = [NSURLConnection sendSynchronousRequest:request returningResponse:&WSresponse error:&WSerror];
}

- (BOOL)connection:(NSURLConnection *)connection canAuthenticateAgainstProtectionSpace:(NSURLProtectionSpace *)protectionSpace {
    return [protectionSpace.authenticationMethod isEqualToString:NSURLAuthenticationMethodServerTrust];
}

- (void)connection:(NSURLConnection *)connection didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge {
    [challenge.sender useCredential:[NSURLCredential credentialForTrust:challenge.protectionSpace.serverTrust] forAuthenticationChallenge:challenge];
    [challenge.sender continueWithoutCredentialForAuthenticationChallenge:challenge];
}
@end

错误是:

Error Domain=NSURLErrorDomain Code=-1202 "The certificate for this server is invalid. You might be connecting to a server that is pretending to be “xyz.com” which could put your confidential information at risk." UserInfo=0x8161600 {NSErrorFailingURLStringKey=https://xyz.com/services/report, NSLocalizedRecoverySuggestion=Would you like to connect to the server anyway?, NSErrorFailingURLKey=https://xyz.com/services/report, NSLocalizedDescription=The certificate for this server is invalid. You might be connecting to a server that is pretending to be “xyz.com” which could put your confidential information at risk., NSUnderlyingError=0x8134cb0 "The certificate for this server is invalid. You might be connecting to a server that is pretending to be “xyz.com” which could put your confidential information at risk.", NSURLErrorFailingURLPeerTrustErrorKey=<SecTrustRef: 0x6d86020>}

非常感谢

twickl

【问题讨论】:

    标签: ios nsurlconnection


    【解决方案1】:

    晚了,但可能对其他人有用

    解决方案链接是http://www.wim.me/nsurlconnection-in-its-own-thread/

    【讨论】:

    • 我遇到了这个确切的问题,并且会花费一整夜来解决
    【解决方案2】:

    我无法回答为什么这些方法没有被触发,我有完全相同的方法并且在使用 HTTPS 时它们会被调用,但我想知道为什么你没有实现 @ 的其余部分987654321@委托方法?如-didReceiveResponse

    我也认为您的连接实际上是在

    NSURLConnection *conn = [[NSURLConnection alloc] initWithRequest:request delegate:self];
    

    你不应该有[NSURLConnection connectionWithRequest:request delegate:self];,最后你正在启动并设置另一个同步 NSURLConnection 并将其存储到应该是 NSMutableData 的位置。

    尝试仅使用您的第一个NSUrlConnection *conn(并确保稍后正确释放它),然后在-didReceiveResponse 委托方法中设置您的数据,您实际上可以在-didReceiveData 委托方法中设置或附加数据.

    【讨论】:

    • 你是对的......现在它可以工作了。使用正确且唯一的实例并调用委托方法!
    猜你喜欢
    • 2012-11-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-08-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多