AFNetworking 2.0 和 HTTP 基本身份验证 [英] AFNetworking 2.0 and HTTP Basic Authentication
问题描述
在 AFNetworking 2.0 上找不到 AFHTTPClient,使用:
Can't find AFHTTPClient on AFNetworking 2.0, to use:
AFHTTPClient *client = [AFHTTPClient clientWithBaseURL:[NSURL URLWithString:@"http://examplewebsite.com]];
[client setAuthorizationHeaderWithUsername:@"username" password:@"password"];
在 AFNetworking 2.0 上需要如何管理?
How it needs to be manage on AFNetworking 2.0?
推荐答案
AFNetworking 2.0 新架构使用序列化程序来创建请求和解析响应.为了设置授权头,首先要初始化一个代替AFHTTPClient的请求操作管理器,创建一个序列化器,然后调用专用的方法来设置头.
AFNetworking 2.0 new architecture use serializers for creating requests and parsing responses. In order to set the authorization header, you should first initialize a request operation manager that replaces the AFHTTPClient, create a serializer and then call the dedicated method to set the header.
例如你的代码会变成:
AFHTTPRequestOperationManager *manager = [[AFHTTPRequestOperationManager alloc] initWithBaseURL:[NSURL URLWithString:@"http://examplewebsite.com"]];
manager.requestSerializer = [AFHTTPRequestSerializer serializer];
[manager.requestSerializer setAuthorizationHeaderFieldWithUsername:@"userName" password:@"password"];
您应该阅读文档和迁移指南,了解 AFNetworking 2.0 版附带的新概念.
You should read the documentation and the migration guide to understand the new concepts that come with the version 2.0 of AFNetworking.
这篇关于AFNetworking 2.0 和 HTTP 基本身份验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!