HTTP 状态代码是否在 iOS SDK 中的任何位置定义?

发布于 2024-11-02 16:36:19 字数 155 浏览 1 评论 0原文

有谁知道 HTTP 状态代码是否/在哪里,如此处指定, iOS SDK 中有定义吗?或者我应该在常量文件中手动重新定义它们?

Does anyone know if/where the HTTP status codes, as specified here, are defined in the iOS SDK? Or should I expect to manually re-define them in a constants file?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

陪我终i 2024-11-09 16:36:19

好吧,它们的定义是

[NSHTTPURLResponse localizedStringForStatusCode:(NSInteger)statusCode]

可以返回给定状态代码的字符串。这就是您要找的吗?

Well, they are defined in the sense that

[NSHTTPURLResponse localizedStringForStatusCode:(NSInteger)statusCode]

can return a string for the given status code. Is that what you are looking for?

终难遇 2024-11-09 16:36:19

我进去了

grep -r '404' *

/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS4.3.sdk/System/Lib‌​rary/Frameworks/ 

却两手空空,所以答案几乎肯定是“不”。

I did a

grep -r '404' *

in

/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS4.3.sdk/System/Lib‌​rary/Frameworks/ 

and came up empty handed so the answer is almost certainly "no".

蓝海似她心 2024-11-09 16:36:19

有一个完整的 Obj-C 库,其中包含迄今为止定义的所有代码:
https://github.com/rafiki270/HTTP-Status-Codes- for-Objective-C

更新
新的 Swift 版本在这里:
https://github.com/manGoweb/StatusCodes

There is a complete Obj-C library with all the codes defined to date:
https://github.com/rafiki270/HTTP-Status-Codes-for-Objective-C

UPDATE:
New Swift version is here:
https://github.com/manGoweb/StatusCodes

蹲在坟头点根烟 2024-11-09 16:36:19

恐怕您必须手动定义它们。我不熟悉 Objective-C,以下是我的快速定义:

HTTP Status Enum in Swift 4 based on https:// /httpstatuses.com/

/// HTTP Statuses
///
/// - continue: Continue
/// - switchingProtocols: Switching Protocols
/// - processing: Processing
/// - ok: OK
/// - created: Created
/// - accepted: Accepted
/// - nonAuthoritativeInformation: Non-authoritative Information
/// - noContent: No Content
/// - resetContent: Reset Content
/// - partialContent: Partial Content
/// - multiStatus: Multi-Status
/// - alreadyReported: Already Reported
/// - iAmUsed: IM Used
/// - multipleChoices: Multiple Choices
/// - movedPermanently: Moved Permanently
/// - found: Found
/// - seeOther: See Other
/// - notModified: Not Modified
/// - useProxy: Use Proxy
/// - temporaryRedirect: Temporary Redirect
/// - permanentRedirect: Permanent Redirect
/// - badRequest: Bad Request
/// - unauthorized: Unauthorized
/// - paymentRequired: Payment Required
/// - forbidden: Forbidden
/// - notFound: Not Found
/// - methodNotAllowed: Method Not Allowed
/// - notAcceptable: Not Acceptable
/// - proxyAuthenticationRequired: Proxy Authentication Required
/// - requestTimeout: Request Timeout
/// - conflict: Conflict
/// - gone: Gone
/// - lengthRequired: Length Required
/// - preconditionFailed: Precondition Failed
/// - payloadTooLarge: Payload Too Large
/// - requestURITooLong: Request-URI Too Long
/// - unsupportedMediaType: Unsupported Media Type
/// - requestedRangeNotSatisfiable: Requested Range Not Satisfiable
/// - expectationFailed: Expectation Failed
/// - iAmATeapot: I'm a teapot
/// - misdirectedRequest: Misdirected Request
/// - unprocessableEntity: Unprocessable Entity
/// - locked: Locked
/// - failedDependency: Failed Dependency
/// - upgradeRequired: Upgrade Required
/// - preconditionRequired: Precondition Required
/// - tooManyRequests: Too Many Requests
/// - requestHeaderFieldsTooLarge: Request Header Fields Too Large
/// - connectionClosedWithoutResponse: Connection Closed Without Response
/// - unavailableForLegalReasons: Unavailable For Legal Reasons
/// - clientClosedRequest: Client Closed Request
/// - internalServerError: Internal Server Error
/// - notImplemented: Not Implemented
/// - badGateway: Bad Gateway
/// - serviceUnavailable: Service Unavailable
/// - gatewayTimeout: Gateway Timeout
/// - httpVersionNotSupported: HTTP Version Not Supported
/// - variantAlsoNegotiates: Variant Also Negotiates
/// - insufficientStorage: Insufficient Storage
/// - loopDetected: Loop Detected
/// - notExtended: Not Extended
/// - networkAuthenticationRequired: Network Authentication Required
/// - networkConnectTimeoutError: Network Connect Timeout Error
enum HttpStatus: Int {
    case `continue` = 100
    case switchingProtocols = 101
    case processing = 102
    case ok = 200
    case created = 201
    case accepted = 202
    case nonAuthoritativeInformation = 203
    case noContent = 204
    case resetContent = 205
    case partialContent = 206
    case multiStatus = 207
    case alreadyReported = 208
    case iAmUsed = 226
    case multipleChoices = 300
    case movedPermanently = 301
    case found = 302
    case seeOther = 303
    case notModified = 304
    case useProxy = 305
    case temporaryRedirect = 307
    case permanentRedirect = 308
    case badRequest = 400
    case unauthorized = 401
    case paymentRequired = 402
    case forbidden = 403
    case notFound = 404
    case methodNotAllowed = 405
    case notAcceptable = 406
    case proxyAuthenticationRequired = 407
    case requestTimeout = 408
    case conflict = 409
    case gone = 410
    case lengthRequired = 411
    case preconditionFailed = 412
    case payloadTooLarge = 413
    case requestURITooLong = 414
    case unsupportedMediaType = 415
    case requestedRangeNotSatisfiable = 416
    case expectationFailed = 417
    case iAmATeapot = 418
    case misdirectedRequest = 421
    case unprocessableEntity = 422
    case locked = 423
    case failedDependency = 424
    case upgradeRequired = 426
    case preconditionRequired = 428
    case tooManyRequests = 429
    case requestHeaderFieldsTooLarge = 431
    case connectionClosedWithoutResponse = 444
    case unavailableForLegalReasons = 451
    case clientClosedRequest = 499
    case internalServerError = 500
    case notImplemented = 501
    case badGateway = 502
    case serviceUnavailable = 503
    case gatewayTimeout = 504
    case httpVersionNotSupported = 505
    case variantAlsoNegotiates = 506
    case insufficientStorage = 507
    case loopDetected = 508
    case notExtended = 510
    case networkAuthenticationRequired = 511
    case networkConnectTimeoutError = 599
}

extension HttpStatus: CustomStringConvertible {
    var description: String {
        // HTTPURLResponse.localizedString(forStatusCode: rawValue)
        return NSLocalizedString(
            "http_status_\(rawValue)",
            tableName: "HttpStatusEnum",
            comment: ""
        )
    }
}

HttpStatusEnum.strings

/// HTTP Status
"http_status_100" = "Continue";
"http_status_101" = "Switching Protocols";
"http_status_102" = "Processing";
"http_status_200" = "OK";
"http_status_201" = "Created";
"http_status_202" = "Accepted";
"http_status_203" = "Non-authoritative Information";
"http_status_204" = "No Content";
"http_status_205" = "Reset Content";
"http_status_206" = "Partial Content";
"http_status_207" = "Multi-Status";
"http_status_208" = "Already Reported";
"http_status_226" = "IM Used";
"http_status_300" = "Multiple Choices";
"http_status_301" = "Moved Permanently";
"http_status_302" = "Found";
"http_status_303" = "See Other";
"http_status_304" = "Not Modified";
"http_status_305" = "Use Proxy";
"http_status_307" = "Temporary Redirect";
"http_status_308" = "Permanent Redirect";
"http_status_400" = "Bad Request";
"http_status_401" = "Unauthorized";
"http_status_402" = "Payment Required";
"http_status_403" = "Forbidden";
"http_status_404" = "Not Found";
"http_status_405" = "Method Not Allowed";
"http_status_406" = "Not Acceptable";
"http_status_407" = "Proxy Authentication Required";
"http_status_408" = "Request Timeout";
"http_status_409" = "Conflict";
"http_status_410" = "Gone";
"http_status_411" = "Length Required";
"http_status_412" = "Precondition Failed";
"http_status_413" = "Payload Too Large";
"http_status_414" = "Request-URI Too Long";
"http_status_415" = "Unsupported Media Type";
"http_status_416" = "Requested Range Not Satisfiable";
"http_status_417" = "Expectation Failed";
"http_status_418" = "I'm a teapot";
"http_status_421" = "Misdirected Request";
"http_status_422" = "Unprocessable Entity";
"http_status_423" = "Locked";
"http_status_424" = "Failed Dependency";
"http_status_426" = "Upgrade Required";
"http_status_428" = "Precondition Required";
"http_status_429" = "Too Many Requests";
"http_status_431" = "Request Header Fields Too Large";
"http_status_444" = "Connection Closed Without Response";
"http_status_451" = "Unavailable For Legal Reasons";
"http_status_499" = "Client Closed Request";
"http_status_500" = "Internal Server Error";
"http_status_501" = "Not Implemented";
"http_status_502" = "Bad Gateway";
"http_status_503" = "Service Unavailable";
"http_status_504" = "Gateway Timeout";
"http_status_505" = "HTTP Version Not Supported";
"http_status_506" = "Variant Also Negotiates";
"http_status_507" = "Insufficient Storage";
"http_status_508" = "Loop Detected";
"http_status_510" = "Not Extended";
"http_status_511" = "Network Authentication Required";
"http_status_599" = "Network Connect Timeout Error";

I'm afraid you'll have to manually define them. I'm not familiar with Objective-C, following is my swift definition:

HTTP Status Enum in Swift 4 based on https://httpstatuses.com/

/// HTTP Statuses
///
/// - continue: Continue
/// - switchingProtocols: Switching Protocols
/// - processing: Processing
/// - ok: OK
/// - created: Created
/// - accepted: Accepted
/// - nonAuthoritativeInformation: Non-authoritative Information
/// - noContent: No Content
/// - resetContent: Reset Content
/// - partialContent: Partial Content
/// - multiStatus: Multi-Status
/// - alreadyReported: Already Reported
/// - iAmUsed: IM Used
/// - multipleChoices: Multiple Choices
/// - movedPermanently: Moved Permanently
/// - found: Found
/// - seeOther: See Other
/// - notModified: Not Modified
/// - useProxy: Use Proxy
/// - temporaryRedirect: Temporary Redirect
/// - permanentRedirect: Permanent Redirect
/// - badRequest: Bad Request
/// - unauthorized: Unauthorized
/// - paymentRequired: Payment Required
/// - forbidden: Forbidden
/// - notFound: Not Found
/// - methodNotAllowed: Method Not Allowed
/// - notAcceptable: Not Acceptable
/// - proxyAuthenticationRequired: Proxy Authentication Required
/// - requestTimeout: Request Timeout
/// - conflict: Conflict
/// - gone: Gone
/// - lengthRequired: Length Required
/// - preconditionFailed: Precondition Failed
/// - payloadTooLarge: Payload Too Large
/// - requestURITooLong: Request-URI Too Long
/// - unsupportedMediaType: Unsupported Media Type
/// - requestedRangeNotSatisfiable: Requested Range Not Satisfiable
/// - expectationFailed: Expectation Failed
/// - iAmATeapot: I'm a teapot
/// - misdirectedRequest: Misdirected Request
/// - unprocessableEntity: Unprocessable Entity
/// - locked: Locked
/// - failedDependency: Failed Dependency
/// - upgradeRequired: Upgrade Required
/// - preconditionRequired: Precondition Required
/// - tooManyRequests: Too Many Requests
/// - requestHeaderFieldsTooLarge: Request Header Fields Too Large
/// - connectionClosedWithoutResponse: Connection Closed Without Response
/// - unavailableForLegalReasons: Unavailable For Legal Reasons
/// - clientClosedRequest: Client Closed Request
/// - internalServerError: Internal Server Error
/// - notImplemented: Not Implemented
/// - badGateway: Bad Gateway
/// - serviceUnavailable: Service Unavailable
/// - gatewayTimeout: Gateway Timeout
/// - httpVersionNotSupported: HTTP Version Not Supported
/// - variantAlsoNegotiates: Variant Also Negotiates
/// - insufficientStorage: Insufficient Storage
/// - loopDetected: Loop Detected
/// - notExtended: Not Extended
/// - networkAuthenticationRequired: Network Authentication Required
/// - networkConnectTimeoutError: Network Connect Timeout Error
enum HttpStatus: Int {
    case `continue` = 100
    case switchingProtocols = 101
    case processing = 102
    case ok = 200
    case created = 201
    case accepted = 202
    case nonAuthoritativeInformation = 203
    case noContent = 204
    case resetContent = 205
    case partialContent = 206
    case multiStatus = 207
    case alreadyReported = 208
    case iAmUsed = 226
    case multipleChoices = 300
    case movedPermanently = 301
    case found = 302
    case seeOther = 303
    case notModified = 304
    case useProxy = 305
    case temporaryRedirect = 307
    case permanentRedirect = 308
    case badRequest = 400
    case unauthorized = 401
    case paymentRequired = 402
    case forbidden = 403
    case notFound = 404
    case methodNotAllowed = 405
    case notAcceptable = 406
    case proxyAuthenticationRequired = 407
    case requestTimeout = 408
    case conflict = 409
    case gone = 410
    case lengthRequired = 411
    case preconditionFailed = 412
    case payloadTooLarge = 413
    case requestURITooLong = 414
    case unsupportedMediaType = 415
    case requestedRangeNotSatisfiable = 416
    case expectationFailed = 417
    case iAmATeapot = 418
    case misdirectedRequest = 421
    case unprocessableEntity = 422
    case locked = 423
    case failedDependency = 424
    case upgradeRequired = 426
    case preconditionRequired = 428
    case tooManyRequests = 429
    case requestHeaderFieldsTooLarge = 431
    case connectionClosedWithoutResponse = 444
    case unavailableForLegalReasons = 451
    case clientClosedRequest = 499
    case internalServerError = 500
    case notImplemented = 501
    case badGateway = 502
    case serviceUnavailable = 503
    case gatewayTimeout = 504
    case httpVersionNotSupported = 505
    case variantAlsoNegotiates = 506
    case insufficientStorage = 507
    case loopDetected = 508
    case notExtended = 510
    case networkAuthenticationRequired = 511
    case networkConnectTimeoutError = 599
}

extension HttpStatus: CustomStringConvertible {
    var description: String {
        // HTTPURLResponse.localizedString(forStatusCode: rawValue)
        return NSLocalizedString(
            "http_status_\(rawValue)",
            tableName: "HttpStatusEnum",
            comment: ""
        )
    }
}

HttpStatusEnum.strings:

/// HTTP Status
"http_status_100" = "Continue";
"http_status_101" = "Switching Protocols";
"http_status_102" = "Processing";
"http_status_200" = "OK";
"http_status_201" = "Created";
"http_status_202" = "Accepted";
"http_status_203" = "Non-authoritative Information";
"http_status_204" = "No Content";
"http_status_205" = "Reset Content";
"http_status_206" = "Partial Content";
"http_status_207" = "Multi-Status";
"http_status_208" = "Already Reported";
"http_status_226" = "IM Used";
"http_status_300" = "Multiple Choices";
"http_status_301" = "Moved Permanently";
"http_status_302" = "Found";
"http_status_303" = "See Other";
"http_status_304" = "Not Modified";
"http_status_305" = "Use Proxy";
"http_status_307" = "Temporary Redirect";
"http_status_308" = "Permanent Redirect";
"http_status_400" = "Bad Request";
"http_status_401" = "Unauthorized";
"http_status_402" = "Payment Required";
"http_status_403" = "Forbidden";
"http_status_404" = "Not Found";
"http_status_405" = "Method Not Allowed";
"http_status_406" = "Not Acceptable";
"http_status_407" = "Proxy Authentication Required";
"http_status_408" = "Request Timeout";
"http_status_409" = "Conflict";
"http_status_410" = "Gone";
"http_status_411" = "Length Required";
"http_status_412" = "Precondition Failed";
"http_status_413" = "Payload Too Large";
"http_status_414" = "Request-URI Too Long";
"http_status_415" = "Unsupported Media Type";
"http_status_416" = "Requested Range Not Satisfiable";
"http_status_417" = "Expectation Failed";
"http_status_418" = "I'm a teapot";
"http_status_421" = "Misdirected Request";
"http_status_422" = "Unprocessable Entity";
"http_status_423" = "Locked";
"http_status_424" = "Failed Dependency";
"http_status_426" = "Upgrade Required";
"http_status_428" = "Precondition Required";
"http_status_429" = "Too Many Requests";
"http_status_431" = "Request Header Fields Too Large";
"http_status_444" = "Connection Closed Without Response";
"http_status_451" = "Unavailable For Legal Reasons";
"http_status_499" = "Client Closed Request";
"http_status_500" = "Internal Server Error";
"http_status_501" = "Not Implemented";
"http_status_502" = "Bad Gateway";
"http_status_503" = "Service Unavailable";
"http_status_504" = "Gateway Timeout";
"http_status_505" = "HTTP Version Not Supported";
"http_status_506" = "Variant Also Negotiates";
"http_status_507" = "Insufficient Storage";
"http_status_508" = "Loop Detected";
"http_status_510" = "Not Extended";
"http_status_511" = "Network Authentication Required";
"http_status_599" = "Network Connect Timeout Error";
与君绝 2024-11-09 16:36:19

http状态码可以由服务器响应来定义。当有连接时,可以使用 NSURLResponse 来读出 statusCode。
这些 4** 响应可以在您的服务器内部定义。

The http status code can be defined by the server response. When there is a connection, you can use the NSURLResponse to read out the statusCode.
Those 4** response can be defined internally on your server.

沙沙粒小 2024-11-09 16:36:19

我也找不到我期望存在的头文件,所以我不得不编写自己的一个。

HTTPStatusCodes.h 包含在 GitHub 上的 nv-ios-http-status 项目中,基于 HTTP 状态码的调度可以如下编写。

#import "HTTPStatusCodes.h"

......

- (void)connection:(NSURLConnection *)connection
  didReceiveResponse:(NSURLResponse *)response
{
    NSHTTPURLResponse *res = (NSHTTPURLResponse *)response;

    switch ([res statusCode])
    {
        // 200 OK
        case kHTTPStatusCodeOK:
            ......;
    }

    ......
}

我希望 HTTPStatusCodes.h 可以节省您和其他开发人员的时间。

I also could not find a header file which I expected to exist, so I had to write one of my own.

With HTTPStatusCodes.h contained in nv-ios-http-status project at GitHub, dispatch based on HTTP status codes can be written like the following.

#import "HTTPStatusCodes.h"

......

- (void)connection:(NSURLConnection *)connection
  didReceiveResponse:(NSURLResponse *)response
{
    NSHTTPURLResponse *res = (NSHTTPURLResponse *)response;

    switch ([res statusCode])
    {
        // 200 OK
        case kHTTPStatusCodeOK:
            ......;
    }

    ......
}

I hope HTTPStatusCodes.h can save time of you and other developers.

姐不稀罕 2024-11-09 16:36:19

还有一个 Swift 挂件可以重用它:

https://github.com/rhodgkins/SwiftHTTPStatusCodes

There is also a Swift pendant to reuse it:

https://github.com/rhodgkins/SwiftHTTPStatusCodes

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文