0

我正在尝试使用 AFNetworking 连接到 https 地址下的 API。自从基于 ASIHTTPRequest 的旧应用程序能够连接以来,我一直收到 404。

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

4

1 回答 1

3

基本上您无需添加任何内容,您只需在需要时添加 http 基本身份验证凭据。这就是我通常使用 http 基本身份验证连接到 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];
于 2013-06-24T12:51:27.047 回答