簡體   English   中英

像Javascript這樣在C ++中訪問HttpStatusCode

[英]Access to HttpStatusCode in C++ like Javascript

我正在ESP上編寫Arduino項目。

在Javascript(客戶端)中,我有以下內容:

HttpStatusCode = { // Top10
  OK: 200,
  Created: 201,
  NoContent: 204,
  NotModified: 304,
  BadRequest: 400,
  Unauthorized: 401,
  Forbidden: 403,
  NotFound: 404,
  Conflict: 409,
  InternalServerError: 500
};

在我的代碼中

   if( this.status == HttpStatusCode.OK ) {
     ...
   }

現在,在C ++(服務器端)中,您如何做同樣的事情?

   if( status == HttpStatusCode.OK ) {
     ...
   }

在C ++ 11和更高版本中,您可以使用作用域enum ,例如:

enum class HttpStatusCode { // Top10
    OK = 200,
    Created = 201,
    NoContent = 204,
    NotModified = 304,
    BadRequest = 400,
    Unauthorized = 401,
    Forbidden = 403,
    NotFound = 404,
    Conflict = 409,
    InternalServerError = 500
};

否則,請使用其中包含static常量的classstruct ,例如:

struct HttpStatusCode {
    static const int OK = 200;
    static const int Created = 201;
    static const int NoContent = 204;
    static const int NotModified = 304;
    static const int BadRequest = 400;
    static const int Unauthorized = 401;
    static const int Forbidden = 403;
    static const int NotFound = 404;
    static const int Conflict = 409;
    static const int InternalServerError = 500;
};

無論哪種方式,您都可以像這樣使用它們:

if( status == HttpStatusCode::OK )

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM