连接到https的要求

时间:2013-06-24 12:43:36

标签: ios ssl https afnetworking

我正在尝试使用AFNetworking连接到https地址下的API。我继续获得404,因为基于ASIHTTPRequest的旧应用程序能够连接。

我是否必须以某种方式将证书文件实施到应用程序中?还应该提供什么?

1 个答案:

答案 0 :(得分:3)

基本上你不需要添加任何内容,只需要添加http基本身份验证凭据。这就是我通常使用http basic auth连接到https api的方式。

//Base URL
NSURL *requestPasswordURL = [NSURL URLWithString:BASEURL];
//Http client with server credentials
AFHTTPClient *httpClient = [[AFHTTPClient alloc] initWithBaseURL:requestURL];
[httpClient setAuthorizationHeaderWithUsername:@"user" password:@"password"];

//Set request parameters for example email
NSDictionary *params = @{@"email": email};
NSMutableURLRequest *request = [httpClient requestWithMethod:@"POST" path:API_REQUEST parameters:params];
request.cachePolicy = NSURLRequestReloadIgnoringCacheData;

//Prepare request
AFHTTPRequestOperation *request = [[AFHTTPRequestOperation alloc] initWithRequest:request];
[requestPasswordOperation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
             //Your code
    }
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
    //Your Code
}];

//call start on your request operation
[request start];