首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >HTTPS服务不工作

HTTPS服务不工作
EN

Stack Overflow用户
提问于 2013-02-22 03:50:43
回答 2查看 5.5K关注 0票数 2

我想使用web服务登录。

我的网站是基于https的。我正在使用下面的教程。

代码语言:javascript
运行
复制
http://agilewarrior.wordpress.com/2012/02/01/how-to-make-http-request-from-iphone-and-parse-json-result/ 

下面的代码运行良好。

代码语言:javascript
运行
复制
responseData = [NSMutableData data];
NSURLRequest *request = [NSURLRequest requestWithURL:
                         [NSURL URLWithString:@"https://maps.googleapis.com/maps/api/place/search/json?location=-33.8670522,151.1957362&radius=500&types=food&name=harbour&sensor=false&key=AIzaSyAbgGH36jnyow0MbJNP4g6INkMXqgKFfHk"]];
[[NSURLConnection alloc] initWithRequest:request delegate:self];

但是我的代码是

代码语言:javascript
运行
复制
responseData = [NSMutableData data];
NSURLRequest *request = [NSURLRequest requestWithURL:
                         [NSURL URLWithString:@"https:myurl/login?userId=username&password=111111"]];
[[NSURLConnection alloc] initWithRequest:request delegate:self];

它会给出错误

代码语言:javascript
运行
复制
Connection failed: Error Domain=NSURLErrorDomain Code=-1202 "The certificate for this server is invalid. You might be connecting to a server that is pretending to be --My URL-- which could put your confidential information at risk." UserInfo=0xa294260 {NSErrorFailingURLStringKey=https://mywebsite/login?userId=username&password=111111, NSLocalizedRecoverySuggestion=Would you like to connect to the server anyway?, NSErrorFailingURLKey=https://mywebsite/login?userId=username&password=111111, NSLocalizedDescription=The certificate for this server is invalid. You might be connecting to a server that is pretending to be --My URL-- which could put your confidential information at risk., NSUnderlyingError=0xa5befc0 "The certificate for this server is invalid. You might be connecting to a server that is pretending to be --My URL-- which could put your confidential information at risk.", NSURLErrorFailingURLPeerTrustErrorKey=<SecTrustRef: 0xa5768d0>}
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2013-02-22 03:54:20

这是您连接到的网站的问题,而不是代码的问题。如果它没有已知的证书,它将被视为安全风险。

票数 1
EN

Stack Overflow用户

发布于 2013-02-22 04:03:09

发生的情况是,您的证书无效(从您的应用程序的角度来看)。如果您想冒这个风险,可以添加以下代码。

代码语言:javascript
运行
复制
- (BOOL)connection:(NSURLConnection *)connection canAuthenticateAgainstProtectionSpace:(NSURLProtectionSpace *)protectionSpace {
    return [protectionSpace.authenticationMethod isEqualToString:NSURLAuthenticationMethodServerTrust];
}

- (void)connection:(NSURLConnection *)connection didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge {
NSArray * trustedHosts = @[@"your domain"];
if ([challenge.protectionSpace.authenticationMethod isEqualToString:NSURLAuthenticationMethodServerTrust])
    if ([trustedHosts containsObject:challenge.protectionSpace.host])
        [challenge.sender useCredential:[NSURLCredential credentialForTrust:challenge.protectionSpace.serverTrust] forAuthenticationChallenge:challenge];

[challenge.sender continueWithoutCredentialForAuthenticationChallenge:challenge];
}

因此该证书将被忽略。(请记住将"your domain“替换为您的域。)

票数 5
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/15010975

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档