3

次のコードを使用して https 投稿リクエストを作成しようとしています。

NSURL *url = [NSURL URLWithString:@"https://portkey.formspring.me/login/"];

//initialize a request from url
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[url       standardizedURL]];

//set http method
[request setHTTPMethod:@"POST"];
//initialize a post data

NSDictionary *postDict = [NSDictionary dictionaryWithObjectsAndKeys:@"username", @"username",
                          @"password", @"password", nil];

NSError *error=nil;

NSData* jsonData = [NSJSONSerialization dataWithJSONObject:postDict
                                                   options:NSJSONWritingPrettyPrinted     error:&error];



[request setValue:@"application/x-www-form-urlencoded; charset=utf-8" forHTTPHeaderField:@"Content-Type"];

//set post data of request
[request setHTTPBody:jsonData];

//initialize a connection from request
NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];

//start the connection
[connection start];

しかし、私は次の応答を得ています。

error Error Domain=NSURLErrorDomain Code=-1202 「このサーバーの証明書は無効です。「portkey.formspring.me」になりすましたサーバーに接続している可能性があり、機密情報が危険にさらされる可能性があります。」UserInfo=0x7564180 {NSLocalizedRecoverySuggestion=サーバーに接続しますか?, NSErrorFailingURLKey= https://portkey.formspring.me/login/、NSLocalizedDescription=このサーバーの証明書は無効です。>「portkey.formspring.me」になりすましているサーバーに接続している可能性があり、機密情報が危険にさらされる可能性があります。, NSUnderlyingError=0x7168a20 "このサーバーの証明書は無効です. 「portkey.formspring.me」になりすましているため、機密情報が危険にさらされる可能性があります。", NSURLErrorFailingURLPeerTrustErrorKey=}

NSURLConnection を使用して投稿リクエストを作成する方法を教えてもらえますか??

前もって感謝します。

4

2 に答える 2

0

これらのデリゲート メソッドNSURLConnection非推奨です

-(BOOL)connection:(NSURLConnection *)connection canAuthenticateAgainstProtectionSpace:(NSURLProtectionSpace *)protectionSpace
-(void)connection:(NSURLConnection *)connection didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge
-(void)connection:(NSURLConnection *)connection didCancelAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge

これらの 3 つの非推奨メソッド-connection:willSendRequestForAuthenticationChallenge:の代わりに使用できます

-(void)connection:(NSURLConnection *)connection willSendRequestForAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge
{
    if ([[challenge protectionSpace] authenticationMethod] == NSURLAuthenticationMethodServerTrust)
    {
        [[challenge sender] useCredential:[NSURLCredential credentialForTrust:[[challenge protectionSpace] serverTrust]] forAuthenticationChallenge:challenge];
    }
}
于 2017-01-07T12:47:37.710 に答える