2010-11-23 4 views
4

NSURLConnectionを使用してhttpsのSoapWebserviceを呼び出すクラスがBackendConnectorです。私は多くの投稿を見つけ、代理人の認証方法を実装しようとしましたが、呼び出されることはなく、Googleで6時間後に私は間違ったことをしません。誰かが私に2つのデリゲートメソッドが呼び出されない理由を教えてください。私はそれぞれのブレークポイントを設定し、シミュレータでXcodeを使ってアプリケーションを起動しましたが、まだエラーが発生し、ブレークポイントはヒットしません。NSURLConnectionデリゲートメソッドは呼び出されません...投稿を読んでたくさん投稿することはできません

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

答えて

6

私はそれらのメソッドが解雇されていない理由は、私が持って答えることができません。まったく同じメソッドとHTTPSを使用するときに呼び出されますが、なぜ残りのNSURLConnectionデリゲートメソッドを実装していないのでしょうか?など-didReceiveResponse

ように私はまた、あなたが[NSURLConnection connectionWithRequest:request delegate:self];を持つべきではありません、あなたの接続が実際に

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

で開始することを考えると、最後に、あなたが起動し、別の同期NSURLConnectionを設定し、NSMutableDataどうあるべきかにそれを格納しています。

-didReceiveResponseデリゲートメソッドでデータのみごNSUrlConnection *conn最初の(そして後で適切にそれを解放するようにしてください)してから、セットアップを使用してみてください、あなたは実際に設定または-didReceiveDataデリゲートメソッドにデータを追加することができます。

+0

です...今では動作します。正しいインスタンスと唯一のインスタンスを使用すると、デリゲートメソッドが呼び出されます! – CaptnCrash

6

後期しかし、それは他の人に役に立つかもしれ

ソリューションリンクはあなたが正しいhttp://www.wim.me/nsurlconnection-in-its-own-thread/

+0

私はこの正確な問題を抱えていて、それを理解するために一晩中かかっていたでしょう – Brodie

関連する問題