【问题标题】:HTTPS post request in IOSIOS中的HTTPS post请求
【发布时间】:2013-05-30 21:36:03
【问题描述】:

我正在尝试使用以下代码发出 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 发出 post 请求??

提前致谢。

【问题讨论】:

  • 我认为你需要在服务器端修复它
  • 错误回答了你的问题,服务器不是他们所说的(就iOS而言)
  • 他们的证书已经过期了一段时间,所以我想他们已经知道了。您确定您连接到正确的网址吗?

标签: ios objective-c


【解决方案1】:

您尝试连接的网站的证书不受信任(尝试访问您在 Chrome 中发布的链接)。

默认情况下,iOS 不会让您连接到提供不受信任证书的站点。如果绝对必要,您可以绕过此检查 - 请参阅此问题:How to use NSURLConnection to connect with SSL for an untrusted cert?

但是,实际修复有问题的证书会好很多很多。

【讨论】:

    【解决方案2】:

    NSURLConnection 的这些委托方法已弃用

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

    您可以使用-connection:willSendRequestForAuthenticationChallenge: 代替这 3 个已弃用方法

    -(void)connection:(NSURLConnection *)connection willSendRequestForAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge
    {
        if ([[challenge protectionSpace] authenticationMethod] == NSURLAuthenticationMethodServerTrust)
        {
            [[challenge sender] useCredential:[NSURLCredential credentialForTrust:[[challenge protectionSpace] serverTrust]] forAuthenticationChallenge:challenge];
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2014-10-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-07-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-04-16
      相关资源
      最近更新 更多