您好,欢迎来到二三娱乐。
搜索
您的当前位置:首页详细解析几个和网络请求有关的类(十九) —— NSURLProt

详细解析几个和网络请求有关的类(十九) —— NSURLProt

来源:二三娱乐

版本记录

版本号 时间
V1.0 2018.03.17

前言

回顾

上一篇讲述了NSURLAuthenticationChallenge这个类的详细信息以及一些注意要点,下面这篇我们就主要看一下NSURLProtectionSpace


Overview

服务器或服务器上的区域,通常称为领域,需要身份验证。

首先看一下该类的基本信息。

保护空间定义了一系列匹配约束条件,用于确定应提供哪种凭证。 例如,如果一个请求提供了请求客户端用户名和密码的NSURLAuthenticationChallenge对象,则应用程序应为挑战的保护空间中指定的特定主机,端口,协议和领域提供正确的用户名和密码。


Topics

1. Creating a protection space - 创建一个保护空间

    • 初始化一个保护空间对象。
    • 创建一个代表proxy server的保护空间对象。

2. Getting protection space properties - 获取保护空间的属性

    • 接受者使用的验证方法。
    • 用于客户端证书认证的可接受证书颁发机构。
    • 接受者的host
    • 指示接收方是否代表代理服务器的布尔值。
    • 接受者的端口
    • 接受者的协议
    • 接受者的代理类型
    • 接收者的身份验证领域
    • 一个布尔值,指示是否可以安全地发送保护空间的凭据。
    • 表示服务器的SSL事务状态。

3. Constants

    • 这些常量描述了返回的保护空间支持的协议。
    • 这些常量描述了在 initWithProxyHost:port:type:realm:authenticationMethod:中使用的支持的代理类型,并由proxyType返回。
    • 这些常数描述了在,
      中使用的可用的验证方法,并由返回。

API

下面我们看一下苹果的API文档,下面我就贴出来并添加注释。

1. NSURLProtectionSpace本类

NS_ASSUME_NONNULL_BEGIN

/*!
   @const NSURLProtectionSpaceHTTP
   @abstract The protocol for HTTP
*/
// HTTP协议

FOUNDATION_EXPORT NSString * const NSURLProtectionSpaceHTTP API_AVAILABLE(macos(10.5), ios(2.0), watchos(2.0), tvos(9.0));


/*!
   @const NSURLProtectionSpaceHTTPS
   @abstract The protocol for HTTPS
*/
// HTTPS协议

FOUNDATION_EXPORT NSString * const NSURLProtectionSpaceHTTPS API_AVAILABLE(macos(10.5), ios(2.0), watchos(2.0), tvos(9.0));


/*!
   @const NSURLProtectionSpaceFTP
   @abstract The protocol for FTP
*/
// FTP协议

FOUNDATION_EXPORT NSString * const NSURLProtectionSpaceFTP API_AVAILABLE(macos(10.5), ios(2.0), watchos(2.0), tvos(9.0));

/*!
    @const NSURLProtectionSpaceHTTPProxy
    @abstract The proxy type for http proxies
*/
// HTTP代理的代理类型

FOUNDATION_EXPORT NSString * const NSURLProtectionSpaceHTTPProxy;

/*!
    @const NSURLProtectionSpaceHTTPSProxy
    @abstract The proxy type for https proxies
*/
// HTTPS代理的代理类型

FOUNDATION_EXPORT NSString * const NSURLProtectionSpaceHTTPSProxy;

/*!
    @const NSURLProtectionSpaceFTPProxy
    @abstract The proxy type for ftp proxies
*/
// FTP代理的代理类型

FOUNDATION_EXPORT NSString * const NSURLProtectionSpaceFTPProxy;

/*!
    @const NSURLProtectionSpaceSOCKSProxy
    @abstract The proxy type for SOCKS proxies
*/
// SOCKS代理的代理类型

FOUNDATION_EXPORT NSString * const NSURLProtectionSpaceSOCKSProxy;

/*!
    @const NSURLAuthenticationMethodDefault
    @abstract The default authentication method for a protocol
*/
// 协议的默认验证方法

FOUNDATION_EXPORT NSString * const NSURLAuthenticationMethodDefault;

/*!
    @const NSURLAuthenticationMethodHTTPBasic
    @abstract HTTP basic authentication. Equivalent to
    NSURLAuthenticationMethodDefault for http.
*/
// HTTP基本验证,和HTTP的NSURLAuthenticationMethodDefault是一样的

FOUNDATION_EXPORT NSString * const NSURLAuthenticationMethodHTTPBasic;

/*!
    @const NSURLAuthenticationMethodHTTPDigest
    @abstract HTTP digest authentication.
*/
// HTTP摘要认证

FOUNDATION_EXPORT NSString * const NSURLAuthenticationMethodHTTPDigest;

/*!
    @const NSURLAuthenticationMethodHTMLForm
    @abstract HTML form authentication. Applies to any protocol.
*/
// HTML表单认证。 适用于任何协议

FOUNDATION_EXPORT NSString * const NSURLAuthenticationMethodHTMLForm;

/*!
   @const NSURLAuthenticationMethodNTLM
   @abstract NTLM authentication.
*/
// NTLM验证

FOUNDATION_EXPORT NSString * const NSURLAuthenticationMethodNTLM API_AVAILABLE(macos(10.5), ios(2.0), watchos(2.0), tvos(9.0));

/*!
   @const NSURLAuthenticationMethodNegotiate
   @abstract Negotiate authentication.
*/
// 协商身份验证

FOUNDATION_EXPORT NSString * const NSURLAuthenticationMethodNegotiate API_AVAILABLE(macos(10.5), ios(2.0), watchos(2.0), tvos(9.0));

/*!
    @const NSURLAuthenticationMethodClientCertificate
    @abstract SSL Client certificate.  Applies to any protocol.
 */
// SSL客户端证书。 适用于任何协议

FOUNDATION_EXPORT NSString * const NSURLAuthenticationMethodClientCertificate API_AVAILABLE(macos(10.6), ios(3.0), watchos(2.0), tvos(9.0));

/*!
    @const NSURLAuthenticationMethodServerTrust
    @abstract SecTrustRef validation required.  Applies to any protocol.
 */
// 需要SecTrustRef验证。 适用于任何协议

FOUNDATION_EXPORT NSString * const NSURLAuthenticationMethodServerTrust API_AVAILABLE(macos(10.6), ios(3.0), watchos(2.0), tvos(9.0));

@class NSURLProtectionSpaceInternal;

/*!
    @class NSURLProtectionSpace
    @discussion This class represents a protection space requiring authentication.
*/

@interface NSURLProtectionSpace : NSObject <NSSecureCoding, NSCopying>
{
@private
    NSURLProtectionSpaceInternal *_internal;
}

/*!
    @method initWithHost:port:protocol:realm:authenticationMethod:
    @abstract Initialize a protection space representing an origin server, or a realm on one
    @param host The hostname of the server
    @param port The port for the server
    @param protocol The sprotocol for this server - e.g. "http", "ftp", "https"
    @param realm A string indicating a protocol-specific subdivision
    of a single host. For http and https, this maps to the realm
    string in http authentication challenges. For many other protocols
    it is unused.
    @param authenticationMethod The authentication method to use to access this protection space -
    valid values include nil (default method), @"digest" and @"form".
    @result The initialized object.
*/
// @abstract 初始化代表原始服务器的保护空间或一个域
// @param realm 指示单个主机的协议特定细分的字符串。 对于http和https,
// 这将映射到http认证挑战中的领域字符串。 对于许多其他协议,它未被使用。
// @param authenticationMethod 用于访问此保护空间的身份验证方法 - 有效值包括nil(默认方法),@“digest”和@“form”

- (instancetype)initWithHost:(NSString *)host port:(NSInteger)port protocol:(nullable NSString *)protocol realm:(nullable NSString *)realm authenticationMethod:(nullable NSString *)authenticationMethod;

/*!
    @method initWithProxyHost:port:type:realm:authenticationMethod:
    @abstract Initialize a protection space representing a proxy server, or a realm on one
    @param host The hostname of the proxy server
    @param port The port for the proxy server
    @param type The type of proxy - e.g. "http", "ftp", "SOCKS"
    @param realm A string indicating a protocol-specific subdivision
    of a single host. For http and https, this maps to the realm
    string in http authentication challenges. For many other protocols
    it is unused.
    @param authenticationMethod The authentication method to use to access this protection space -
    valid values include nil (default method) and @"digest"
    @result The initialized object.
*/
// 这个也是初始化的一个方法

- (instancetype)initWithProxyHost:(NSString *)host port:(NSInteger)port type:(nullable NSString *)type realm:(nullable NSString *)realm  authenticationMethod:(nullable NSString *)authenticationMethod;

/*!
    @abstract Get the authentication realm for which the protection space that
    needs authentication
    @discussion This is generally only available for http
    authentication, and may be nil otherwise.
    @result The realm string
*/
// 获取需要身份验证的保护空间的身份验证领域。
// 这通常只适用于http身份验证,并且可能是nil
// 返回值就是域字符串

@property (nullable, readonly, copy) NSString *realm;

/*!
    @abstract Determine if the password for this protection space can be sent securely
    @result YES if a secure authentication method or protocol will be used, NO otherwise
*/
// 确定是否可以安全地发送此保护空间的密码
// YES,如果安全验证方法或协议将被使用,否则就是NO

@property (readonly) BOOL receivesCredentialSecurely;

/*!
    @abstract Determine if this authenticating protection space is a proxy server
    @result YES if a proxy, NO otherwise
*/
// 确定此身份验证保护空间是否是代理服务器,YES代表就是代理服务器,否则为NO

@property (readonly) BOOL isProxy;

/*!
    @abstract Get the proxy host if this is a proxy authentication, or the host from the URL.
    @result The host for this protection space.
*/
// 如果这是代理身份验证,或者来自URL的主机,就获取代理主机

@property (readonly, copy) NSString *host;

/*!
    @abstract Get the proxy port if this is a proxy authentication, or the port from the URL.
    @result The port for this protection space, or 0 if not set.
*/
// 如果这是代理身份验证,或者来自URL的端口,就获取代理端口,返回结果就是保护空间的端口,如果没有设置就返回0

@property (readonly) NSInteger port;

/*!
    @abstract Get the type of this protection space, if a proxy
    @result The type string, or nil if not a proxy.
 */
// 如果是代理,就获取这个保护空间的类型。返回结果就是类型字符串,如果不是代理就返回nil

@property (nullable, readonly, copy) NSString *proxyType;

/*!
    @abstract Get the protocol of this protection space, if not a proxy
    @result The type string, or nil if a proxy.
*/
// 如果不设置代理,获取保护空间的协议。返回结果就是类型字符串,如果不是代理就返回nil

@property (nullable, readonly, copy) NSString *protocol;

/*!
    @abstract Get the authentication method to be used for this protection space
    @result The authentication method
*/
// 获取这个保护空间要使用的身份验证方法

@property (readonly, copy) NSString *authenticationMethod;

@end

2. NSURLProtectionSpace分类NSClientCertificateSpace

/*!
    @discussion This category supplies additional information for use when a client certificate is required by the server in order to complete authentication.
 */
// 当服务器需要客户端证书以完成身份验证时,此类别提供附加信息以供使用。
@interface NSURLProtectionSpace(NSClientCertificateSpace)

/*!
    @abstract Returns an array of acceptable certificate issuing authorities for client certification authentication. Issuers are identified by their distinguished name and returned as a DER encoded data.
    @result An array of NSData objects.  (Nil if the authenticationMethod is not NSURLAuthenticationMethodClientCertificate)
 */
// @abstract 返回用于客户端认证认证的可接受证书颁发机构的数组。 发行人以其专有名称标识并作为DER编码数据返回。
// @result 一个NSData对象数组。 (如果authenticationMethod不是NSURLAuthenticationMethodClientCertificate则为nil)

@property (nullable, readonly, copy) NSArray<NSData *> *distinguishedNames API_AVAILABLE(macos(10.6), ios(3.0), watchos(2.0), tvos(9.0));

@end

3. NSURLProtectionSpace分类NSServerTrustValidationSpace

/*!
    @discussion This category supplies additional information for use by the client to evaluate whether to trust a given server during a security handshake.
 */
// 此类别提供了供客户端用于评估在安全握手期间是否信任给定服务器的附加信息

@interface NSURLProtectionSpace(NSServerTrustValidationSpace)

/*!
    @abstract Returns a SecTrustRef which represents the state of the servers SSL transaction state
    @result A SecTrustRef from Security.framework.  (Nil if the authenticationMethod is not NSURLAuthenticationMethodServerTrust)
 */
// 返回表示服务器SSL事务状态状态的SecTrustRef
// 来自Security.framework的SecTrustRef。 (如果authenticationMethod不是NSURLAuthenticationMethodServerTrust则为零)

@property (nullable, readonly) SecTrustRef serverTrust API_AVAILABLE(macos(10.6), ios(3.0), watchos(2.0), tvos(9.0));

@end

后记

本篇主要讲述的就是NSURLProtectionSpace类的详细信息,喜欢的给个关注和赞,谢谢~~~

Copyright © 2019- yule263.com 版权所有 湘ICP备2023023988号-1

违法及侵权请联系:TEL:199 1889 7713 E-MAIL:2724546146@qq.com

本站由北京市万商天勤律师事务所王兴未律师提供法律服务