iOS:如何建立安全的HTTPS连接以传递凭据? [英] iOS: How to make a secure HTTPS connection to pass credentials?

查看:100
本文介绍了iOS:如何建立安全的HTTPS连接以传递凭据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建我的第一个iPad应用程序。我有一个Web应用程序,我想要以RESTful方式进行身份验证并从中提取数据。

I am creating my first iPad app. I have a web application that I would like to authenticate against and pull data from in a RESTful way.

如果您在浏览器中打开URL( https://myapp.com/auth/login ),您将收到一个表格,用于输入您的用户名和密码。我以为我可以在请求的发布数据中设置登录凭据并提交数据。

If you open up the URL in the browser (https://myapp.com/auth/login), you will get a form to enter your username and password. I was thinking I could set the login credentials in the post data of the request and submit the data.

该网站使用HTTPS进行登录,以便不传递凭据互联网上的纯文本。

The site uses HTTPS for login so that credentials aren't passed in plain text over the internet.

如何建立安全的HTTPS连接以传递凭据?请记住我是否已登录以备将来使用?最好的方法是什么?

How can I make a secure HTTPS connection to pass credentials? Will this remember that I am logged in for future requests? What is the best way to do this?

推荐答案

2013年10月进一步更新

虽然当时我写了这个答案,ASIHTTPRequest得到了广泛的支持,但现在不再是这样了。不建议用于新项目 - 而是直接使用NSURLConnection,或使用AFNetworking。

Although at the time I wrote this answer, ASIHTTPRequest was maintained a widely supported, this is no longer the case. It is not recommended for new projects - instead use NSURLConnection directly, or use AFNetworking.

使用AFNetworking,有一个 [httpClient setAuthorizationHeaderWithUsername:username password :密码]; 用于http身份验证的方法,并创建一个POST样式表单同样容易 - 请参阅 AFNetworking Post Request

With AFNetworking, there is a [httpClient setAuthorizationHeaderWithUsername:username password:password]; method for http authentication, and create a POST style form is equally easy - see AFNetworking Post Request for that.

原始答案:

很多人使用ASIHTTPRequest类来处理http&在iPhone / iPad上使用https,因为它具有很多有用的功能,使用内置类很难或很费时间实现:

A lot of people use the ASIHTTPRequest class to deal with http & https on the iPhone/iPad, as it has a lot of useful features that are difficult or time consuming to achieve with the built in classes:

http://allseeing-i.com/ASIHTTPRequest/

从最简单的级别开始,您将从以下内容开始:

Starting at the simplest level you'd start with something like:

  NSURL *url = [NSURL URLWithString:@"http://allseeing-i.com"];
  ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];

  [request startSynchronous];
  NSError *error = [request error];
  if (!error) {
      NSString *response = [request responseString];
      NSLog(@"response = %@", response);
  }

如果您正在使用HTTP身份验证,ASIHTTPRequest将自动提示用户用户名和密码。

If you're using HTTP authentication, ASIHTTPRequest will automatically prompt the user for the username and password.

如果您正在使用其他形式的身份验证,您可能需要自己请求用户输入用户名和密码,并将其作为POST值提交或者自定义http标头,然后响应可以包含JSON或XML响应中的标记,也可以设置cookie。

IF you're using some other form of authentication you probably need to request the username and password from the user yourself, and submit them as a POST value or a custom http header, and then the response may either include a token in a JSON or XML response, or it could set a cookie.

如果添加更多详细信息,关于认证方案如何运作我可以更具体一点。

If you add more details as to how the authentication scheme works I can be a bit more specific.

更新

根据更新,要模拟POST表单,您只需要添加以下行:

Based on the update, to emulate a POST form you'd just need to add lines like:

[request addPostValue:usernameString forKey:@"username"];
[request addPostValue:passwordString forKey:@"password"];

您还需要更改创建请求的方式,而不是:

You will also need to change the way you create the request, instead of:

  ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];

do:

  ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url];

(我也忘记在上面提到它,我之前提到的代码是使用同步请求,所以你想用一个异步的替换它,以避免在你证明它工作后阻止你。)

(I also forget to mention it above, there's code I put earlier is using a synchronous request, so you'd want to replace it with an asynchronous one to avoid blocking the UI once you'd proved it was working.)

这里有一个iphone的JSON框架:

There's a JSON framework for the iphone here:

http:// code。 google.com/p/json-framework/

这对我很有用,并且很容易与ASIHTTPRequest一起使用。

which works well for me and is simple to use with ASIHTTPRequest.

这篇关于iOS:如何建立安全的HTTPS连接以传递凭据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆