3

SSL証明書の処理をhttpsサービスへの同期接続に追加する方法を誰かが理解するのを手伝ってくれるかどうか疑問に思っていました.

非同期接続でこれを行う方法は知っていますが、同期ではありません。

                NSString *URLpath = @"https://mydomain.com/";
    NSURL *myURL = [[NSURL alloc] initWithString:URLpath];
    NSMutableURLRequest *myURLRequest = [NSMutableURLRequest requestWithURL:myURL cachePolicy:NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:60];
    [myURL release];
    [myURLRequest setHTTPMethod:@"POST"];

    NSString *httpBodystr = @"setting1=1";
    [myURLRequest setHTTPBody:[httpBodystr dataUsingEncoding:NSUTF8StringEncoding]];

    NSHTTPURLResponse* myURLResponse; 
    NSError* myError;
    NSData* myDataResult = [NSURLConnection sendSynchronousRequest:myURLRequest returningResponse:&myURLResponse error:&myError];

            //I guess I am meant to put some SSL handling code here

ありがとうございました。

4

3 に答える 3

-1

同様の問題がありました。私の場合、任意の証明書を受け入れることができる 2 つのデリゲート メソッドを使用して、必要に応じて ssl で動作する非同期接続がありました。

- (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];
}

しかし、私は同期的に同じことをすることに固執していました。あなたの投稿と、残念ながら NSURLConnection で同期呼び出しを実行できず、ssl を操作できないことがほのめかされている別のstackoverflow の投稿が見つかるまで、私は Web を検索しました(ssl 認証プロセスを処理するデリゲートがないため)。私がやったことは、ASIHTTPRequestを取得してそれを使用することです。それは簡単で、セットアップに約1時間かかり、完璧に機能しました. これが私がそれを使用する方法です。

+ (NSString *) getSynchronously:(NSDictionary *)parameters {
    NSURL *url = [NSURL URLWithString:@"https://localhost:8443/MyApp/";
    ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url];
    NSString *parameterJSONString = [parameters JSONRepresentation];
    [request appendPostString:parameterJSONString];
    [request addRequestHeader:@"User-Agent" value:@"MyAgent"];
    request.timeOutSeconds = CONNECTION_TIME_OUT_INTERVAL;
    [request setValidatesSecureCertificate:NO];
    [request startSynchronous];

    NSString *responseString = [request responseString];
    if (request.error) {
        NSLog(@"Server connection failed: %@", [request.error localizedDescription]);
    } else {
        NSLog(@"Server response: %@", responseString);
    }

    return responseString;
}

もちろん重要な部分は、

[request setValidatesSecureCertificate:NO];

もう 1 つの方法は、上記の 2 つの方法を使用して非同期接続を使用して別のスレッドでダウンロードを処理し、要求が完了するまで同期接続が必要なスレッドをブロックすることです。

于 2011-02-22T10:13:50.030 に答える
-2

以下のコードを使用して、これに対する解決策を見つけようとしています。これは機能しますが、おそらくこれをコーディングする方法で何か間違ったことをしており、使用されている方法をよく理解していないため、クラッシュすることがよくあります。しかし、これを改善する方法について何か提案があれば、投稿してください。

行の直後:

NSError* myError;

そして行の直前:

NSData* myDataResult = [NSURLConnection sendSynchronousRequest:myURLRequest       
returningResponse:&myURLResponse error:&myError];

追加:

    int failureCount = 0;
    NSURLProtectionSpace *protectionSpace = [[NSURLProtectionSpace alloc]      
    initWithHost:@"mydomain.com" port:443 protocol:@"https"  realm:nil  
    authenticationMethod:NSURLAuthenticationMethodServerTrust];

    NSURLResponse *response = [[NSURLResponse alloc] initWithURL:myURL MIMEType:@"text/html" 
    expectedContentLength:-1 textEncodingName:nil]; 

    NSURLAuthenticationChallenge *challange = [[NSURLAuthenticationChallenge alloc] 
    initWithProtectionSpace:protectionSpace proposedCredential:[NSURLCredential 
    credentialForTrust:protectionSpace.serverTrust] previousFailureCount:failureCount 
    failureResponse:response error:myError sender:nil];
于 2010-08-28T12:18:01.393 に答える