AFNetworking 2.0和HTTP基本身份验证 [英] AFNetworking 2.0 and HTTP Basic Authentication

查看:193
本文介绍了AFNetworking 2.0和HTTP基本身份验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在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.

例如,您的代码将变为:

For example you code would become:

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屋!

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