AFNetworking 网络请求失败后通过 NSError 判
2018-03-02 本文已影响251人
贝勒老爷
错误码
这里给出苹果提供的所有网络请求的错误码,可以根据不同错误码来给出不同的错误提示以及给出相应的操作,想具体了解每个错误码对应什么意思可以自行查询一下官方文档。
/*!
@enum NSURL-related Error Codes
@abstract Constants used by NSError to indicate errors in the NSURL domain
*/
NS_ENUM(NSInteger)
{
NSURLErrorUnknown = -1,
NSURLErrorCancelled = -999,
NSURLErrorBadURL = -1000,
NSURLErrorTimedOut = -1001,
NSURLErrorUnsupportedURL = -1002,
NSURLErrorCannotFindHost = -1003,
NSURLErrorCannotConnectToHost = -1004,
NSURLErrorNetworkConnectionLost = -1005,
NSURLErrorDNSLookupFailed = -1006,
NSURLErrorHTTPTooManyRedirects = -1007,
NSURLErrorResourceUnavailable = -1008,
NSURLErrorNotConnectedToInternet = -1009,
NSURLErrorRedirectToNonExistentLocation = -1010,
NSURLErrorBadServerResponse = -1011,
NSURLErrorUserCancelledAuthentication = -1012,
NSURLErrorUserAuthenticationRequired = -1013,
NSURLErrorZeroByteResource = -1014,
NSURLErrorCannotDecodeRawData = -1015,
NSURLErrorCannotDecodeContentData = -1016,
NSURLErrorCannotParseResponse = -1017,
NSURLErrorAppTransportSecurityRequiresSecureConnection NS_ENUM_AVAILABLE(10_11, 9_0) = -1022,
NSURLErrorFileDoesNotExist = -1100,
NSURLErrorFileIsDirectory = -1101,
NSURLErrorNoPermissionsToReadFile = -1102,
NSURLErrorDataLengthExceedsMaximum NS_ENUM_AVAILABLE(10_5, 2_0) = -1103,
NSURLErrorFileOutsideSafeArea API_AVAILABLE(macosx(10.12.4), ios(10.3), watchos(3.2), tvos(10.2)) = -1104,
// SSL errors
NSURLErrorSecureConnectionFailed = -1200,
NSURLErrorServerCertificateHasBadDate = -1201,
NSURLErrorServerCertificateUntrusted = -1202,
NSURLErrorServerCertificateHasUnknownRoot = -1203,
NSURLErrorServerCertificateNotYetValid = -1204,
NSURLErrorClientCertificateRejected = -1205,
NSURLErrorClientCertificateRequired = -1206,
NSURLErrorCannotLoadFromNetwork = -2000,
// Download and file I/O errors
NSURLErrorCannotCreateFile = -3000,
NSURLErrorCannotOpenFile = -3001,
NSURLErrorCannotCloseFile = -3002,
NSURLErrorCannotWriteToFile = -3003,
NSURLErrorCannotRemoveFile = -3004,
NSURLErrorCannotMoveFile = -3005,
NSURLErrorDownloadDecodingFailedMidStream = -3006,
NSURLErrorDownloadDecodingFailedToComplete =-3007,
NSURLErrorInternationalRoamingOff NS_ENUM_AVAILABLE(10_7, 3_0) = -1018,
NSURLErrorCallIsActive NS_ENUM_AVAILABLE(10_7, 3_0) = -1019,
NSURLErrorDataNotAllowed NS_ENUM_AVAILABLE(10_7, 3_0) = -1020,
NSURLErrorRequestBodyStreamExhausted NS_ENUM_AVAILABLE(10_7, 3_0) = -1021,
NSURLErrorBackgroundSessionRequiresSharedContainer NS_ENUM_AVAILABLE(10_10, 8_0) = -995,
NSURLErrorBackgroundSessionInUseByAnotherProcess NS_ENUM_AVAILABLE(10_10, 8_0) = -996,
NSURLErrorBackgroundSessionWasDisconnected NS_ENUM_AVAILABLE(10_10, 8_0)= -997,
};
有关于网络请求失败的解释:
case -1://NSURLErrorUnknown
errorMesg = @"无效的URL地址";
break;
case -999://NSURLErrorCancelled
errorMesg = @"无效的URL地址";
break;
case -1000://NSURLErrorBadURL
errorMesg = @"无效的URL地址";
break;
case -1001://NSURLErrorTimedOut
errorMesg = @"网络不给力,请稍后再试";
break;
case -1002://NSURLErrorUnsupportedURL
errorMesg = @"不支持的URL地址";
break;
case -1003://NSURLErrorCannotFindHost
errorMesg = @"找不到服务器";
break;
case -1004://NSURLErrorCannotConnectToHost
errorMesg = @"连接不上服务器";
break;
case -1103://NSURLErrorDataLengthExceedsMaximum
errorMesg = @"请求数据长度超出最大限度";
break;
case -1005://NSURLErrorNetworkConnectionLost
errorMesg = @"网络连接异常";
break;
case -1006://NSURLErrorDNSLookupFailed
errorMesg = @"DNS查询失败";
break;
case -1007://NSURLErrorHTTPTooManyRedirects
errorMesg = @"HTTP请求重定向";
break;
case -1008://NSURLErrorResourceUnavailable
errorMesg = @"资源不可用";
break;
case -1009://NSURLErrorNotConnectedToInternet
errorMesg = @"无网络连接";
break;
case -1010://NSURLErrorRedirectToNonExistentLocation
errorMesg = @"重定向到不存在的位置";
break;
case -1011://NSURLErrorBadServerResponse
errorMesg = @"服务器响应异常";
break;
case -1012://NSURLErrorUserCancelledAuthentication
errorMesg = @"用户取消授权";
break;
case -1013://NSURLErrorUserAuthenticationRequired
errorMesg = @"需要用户授权";
break;
case -1014://NSURLErrorZeroByteResource
errorMesg = @"零字节资源";
break;
case -1015://NSURLErrorCannotDecodeRawData
errorMesg = @"无法解码原始数据";
break;
case -1016://NSURLErrorCannotDecodeContentData
errorMesg = @"无法解码内容数据";
break;
case -1017://NSURLErrorCannotParseResponse
errorMesg = @"无法解析响应";
break;
case -1018://NSURLErrorInternationalRoamingOff
errorMesg = @"国际漫游关闭";
break;
case -1019://NSURLErrorCallIsActive
errorMesg = @"被叫激活";
break;
case -1020://NSURLErrorDataNotAllowed
errorMesg = @"数据不被允许";
break;
case -1021://NSURLErrorRequestBodyStreamExhausted
errorMesg = @"请求体";
break;
case -1100://NSURLErrorFileDoesNotExist
errorMesg = @"文件不存在";
break;
case -1101://NSURLErrorFileIsDirectory
errorMesg = @"文件是个目录";
break;
case -1102://NSURLErrorNoPermissionsToReadFile
errorMesg = @"无读取文件权限";
break;
case -1200://NSURLErrorSecureConnectionFailed
errorMesg = @"安全连接失败";
break;
case -1201://NSURLErrorServerCertificateHasBadDate
errorMesg = @"服务器证书失效";
break;
case -1202://NSURLErrorServerCertificateUntrusted
errorMesg = @"不被信任的服务器证书";
break;
case -1203://NSURLErrorServerCertificateHasUnknownRoot
errorMesg = @"未知Root的服务器证书";
break;
case -1204://NSURLErrorServerCertificateNotYetValid
errorMesg = @"服务器证书未生效";
break;
case -1205://NSURLErrorClientCertificateRejected
errorMesg = @"客户端证书被拒";
break;
case -1206://NSURLErrorClientCertificateRequired
errorMesg = @"需要客户端证书";
break;
case -2000://NSURLErrorCannotLoadFromNetwork
errorMesg = @"无法从网络获取";
break;
case -3000://NSURLErrorCannotCreateFile
errorMesg = @"无法创建文件";
break;
case -3001:// NSURLErrorCannotOpenFile
errorMesg = @"无法打开文件";
break;
case -3002://NSURLErrorCannotCloseFile
errorMesg = @"无法关闭文件";
break;
case -3003://NSURLErrorCannotWriteToFile
errorMesg = @"无法写入文件";
break;
case -3004://NSURLErrorCannotRemoveFile
errorMesg = @"无法删除文件";
break;
case -3005://NSURLErrorCannotMoveFile
errorMesg = @"无法移动文件";
break;
case -3006://NSURLErrorDownloadDecodingFailedMidStream
errorMesg = @"下载解码数据失败";
break;
case -3007://NSURLErrorDownloadDecodingFailedToComplete
errorMesg = @"下载解码数据失败";
break;