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

时间:2011-04-22 04:10:01

标签: ios objective-c http-status-codes

是否有人知道在iOS SDK中是否定义了HTTP状态代码as specified here?或者我是否应该在常量文件中手动重新定义它们?

7 个答案:

答案 0 :(得分:26)

嗯,它们的定义是

[NSHTTPURLResponse localizedStringForStatusCode:(NSInteger)statusCode]

可以返回给定状态代码的字符串。那是你在找什么?

答案 1 :(得分:26)

我做了

grep -r '404' *

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

然后空手而归,所以答案肯定是“不”。

答案 2 :(得分:6)

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

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

答案 3 :(得分:2)

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

答案 4 :(得分:2)

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

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

#import "HTTPStatusCodes.h"

......

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

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

    ......
}

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

答案 5 :(得分:1)

还有一个Swift吊坠可以重复使用它:

https://github.com/rhodgkins/SwiftHTTPStatusCodes

答案 6 :(得分:0)

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

Swift 4中基于https://httpstatuses.com/的HTTP状态枚举

with app.test_client() as client:
    json.loads(client.get("the/url").get_data().decode("utf-8"))
    # ...

/// 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