【问题标题】:iPhone - SSL connectioniPhone - SSL 连接
【发布时间】:2009-11-25 20:11:42
【问题描述】:

学习通过 iphone 连接到 ssl Web 服务的最佳起点是什么?

到目前为止,我通过 SOAP 等通过 http 进行了一些基本连接,但我没有使用 https 的经验。任何好的资源、教程、起始参考、“使用 nsurl...class”都表示赞赏

【问题讨论】:

    标签: iphone cocoa-touch web-services ssl


    【解决方案1】:

    NSURLConnection 默认使用 SSL,可以访问 https 站点。关于让用户信任 SSL 证书可能会出现问题,here 对此进行了讨论,我发现这很有趣。

    【讨论】:

      【解决方案2】:

      我正在发布一个示例 https 客户端。如果服务器证书无效,它会忽略。 服务器有一个带有 uritemplate=username({usercode})/password({passcode})

      的 webget 方法

      您可以使用 CharlesProxy 来检查您的外发消息

      #import "Hello_SOAPViewController.h"
      @interface NSURLRequest (withHttpsCertificates)
      + (BOOL)allowsAnyHTTPSCertificateForHost:(NSString*)host;
      + (void)setAllowsAnyHTTPSCertificate:(BOOL)allow forHost:(NSString*)host;
      @end
      
      @implementation Hello_SOAPViewController
      
      
      NSMutableData *webData;
      
      - (void)viewDidLoad {
      
      //////////////////////////////////////////////////
      
      //Web Service Call
      
      //////////////////////////////////////////////////
      
          NSURL *url = [NSURL URLWithString:@"https://192.168.1.105/HelloService/Service.svc/username(user)/password(xxx)"];                           
      
          NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:url cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:60.0];
          [NSURLRequest setAllowsAnyHTTPSCertificate:YES forHost:[url host]];
      
          [theRequest addValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];       
      
          [theRequest setHTTPMethod:@"GET"];     
          NSURLConnection *theConnection = [[NSURLConnection alloc] initWithRequest:theRequest delegate:self];
      
          if(theConnection) {
              webData = [[NSMutableData data] retain];
          }
          else {
              NSLog(@"theConnection is NULL");
          }
      
      }
      
      
      
      -(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
      {
          [webData setLength: 0];
      }
      -(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
      {
          [webData appendData:data];
      }
      -(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
      {
      
          NSLog(@"ERROR with theConnection:%@",[error description]);
          if ([error code] == -1001 ){//isEqualToString:@"timed out"]) {
              UIAlertView *alertView = [[[UIAlertView alloc] initWithTitle:@"Connection Error" message:@"Server Unresponsive"  delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil] autorelease];
              [alertView show];
      
          }else{
              UIAlertView *alertView = [[[UIAlertView alloc] initWithTitle:@"Connection Error" message:@"Check your internet connection "  delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil] autorelease];
              [alertView show];
          }
      
      
          [connection release];
          [webData release];
      }
      -(void)connectionDidFinishLoading:(NSURLConnection *)connection
      {
          NSLog(@"DONE. Received Bytes: %d", [webData length]);
      
          ///////////////////////
          //Process Your Data here:
      
      
      
      
      
      
          ///////////////////////
      
          [connection release];
          [webData release];
      
      }
      
      
      - (void)didReceiveMemoryWarning {
          // Releases the view if it doesn't have a superview.
          [super didReceiveMemoryWarning];
      
          // Release any cached data, images, etc that aren't in use.
      }
      
      - (void)viewDidUnload {
          // Release any retained subviews of the main view.
          // e.g. self.myOutlet = nil;
      }
      
      
      - (void)dealloc {
      
          [super dealloc];
      }
      

      【讨论】:

        【解决方案3】:

        查看ASIHTTPRequest。它非常稳定、无泄漏、易于使用,并且包括许多好东西,例如下载文件恢复、进度条支持等……它还支持身份验证

        【讨论】:

        • 身份验证是否与加密一样安全,或者我如何理解这一点?谢谢
        • 这是一个非常古老的答案,不再维护 ASIHTTPRequest。试试github.com/AFNetworking/AFNetworking
        猜你喜欢
        • 2011-02-09
        • 2011-01-19
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2010-11-29
        • 2011-06-29
        • 2015-09-03
        相关资源
        最近更新 更多