不会调用NSURLConnection委托方法......无法弄清楚并阅读了很多帖子

4 nsurlconnection ios

我有一个名为calls的类BackendConnector,它使用a NSURLConnection来调用SoapWebservicehttps.我找到了很多帖子,并尝试实施代理方法有关身份验证,但他们不会被调用,在谷歌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
Run Code Online (Sandbox Code Playgroud)

错误是:

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>}
Run Code Online (Sandbox Code Playgroud)

非常感谢你

twickl

小智 6

迟到但对其他人可能有用

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


leu*_*ima 5

我将无法回答为什么这些方法没有被解雇,我有完全相同的方法,并在使用HTTPS时调用它们,但我想知道为什么你没有实现其余的NSURLConnection委托方法?如-didReceiveResponse

我也认为你的连接实际上是在启动

NSURLConnection *conn = [[NSURLConnection alloc] initWithRequest:request delegate:self];
Run Code Online (Sandbox Code Playgroud)

你不应该[NSURLConnection connectionWithRequest:request delegate:self];,最后你开始并设置另一个同步NSURLConnection并将其存储到应该是什么NSMutableData.

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