iPhone development: How to implement HTTPS connection?

依然范特西╮ 提交于 2020-01-01 03:56:39

问题


I am working on an iPhone project which need to connect to the IIS server over HTTPS or SSL protocol. Can anyone tell me how to implement HTTPS connection in iPhone? Any advice would be appreciate.

Thank you in advance.


回答1:


Ben Copsey's ASIHTTPRequest framework makes it trivially easy to issue web requests over SSL.

Modifying the site's example slightly:

NSURL *url = [NSURL URLWithString:@"https://allseeing-i.com"];
ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
[request startSynchronous];
NSError *error = [request error];
if (!error) {
    NSString *response = [request responseString];
    NSLog (@"%@", response);
}



回答2:


Just use NSURLConnection with an NSURLRequest pointing to an NUSUL with https protocol, just like that:

NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"https://mySecureServer.net"]];
NSURLResponse *response = nil;
NSError *error = nil;
NSData *result = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
if (!error) {
    //do something with response & data
}

But I suggest you have a look at the documentation of the URL loading system in Cocoa.




回答3:


Jason don't forget to implement the connection:canAuthenticateAgainsProtectionSpace and connection:didReceiveAuthenticationChallenge to make sure you're handling the challenges for the Server authentication and Client Authentication:

check out this post that will help you with this https://devforums.apple.com/message/143091#143091




回答4:


You probably don't have a public certificate. Can you access the https resource with firefox/safari/chrome without an error? if not - that's the problem, not your code.



来源:https://stackoverflow.com/questions/2809363/iphone-development-how-to-implement-https-connection

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!