【问题标题】:HTTP Get from a C++ server从 C++ 服务器获取 HTTP
【发布时间】:2018-06-28 13:15:43
【问题描述】:

我有一个 Angular 客户端,我的服务器是 C++。我知道使用节点更好,但使用 C++ 服务器是一个限制。我正在使用我在 GitHub 上找到的 server。当我执行 httpClient.get 请求时,它会返回错误。

服务器中的代码:

int send_response(int fd, char *header, char *content_type, char *body) {
   const int max_response_size = 65536;
   char response[max_response_size];

   // Get current time for the HTTP header
   time_t t1 = time(NULL);
   struct tm *ltime = localtime(&t1);

   // How many bytes in the body
   int content_length = strlen(body);

   int response_length = sprintf(response,
       "%s\r\n"
       "Access-Control-Allow-Origin: %s\r\n"
       "Access-Control-Allow-Methods: %s\r\n"
       "Content-Length: %d\r\n"
       "Content-Type: %s\r\n"
       "Date: %s" // asctime adds its own newline
       "Connection: close\r\n"
       "\r\n" // End of HTTP header
       "%s",

       header,
       "*",
       "POST, GET",
       content_length,
       content_type,
       asctime(ltime),
       body
    );

   // Send it all!
   int rv = send(fd, response, response_length, 0);

  if (rv < 0) 
      perror("send");

   return rv;
}

void get_d20(int fd) {
    char response_body[8];
    sprintf(response_body, "%d", 5);

    send_response(fd, "HTTP/1.1 200 OK", "text/plain", response_body);
}

这是我的客户端代码:

public my_function(): void {
   let theResp;
   this.getServer().subscribe((resp) => {
       theResp = resp;
   });
   console.log('Return of get: ' + theResp);
}

public getServer(): Observable<any> {
   const httpOptions = {
    headers: new HttpHeaders({
      'Authorization': '*'
    })
   }
   return this.httpClient.get('http://SERVER_IP_ADRESS:3490/d20', httpOptions);
}

每次尝试都会遇到同样的错误:

zone.js:2969 OPTIONS http://SERVER_IP_ADRESS:3490/d20 0 ()
core.js:1601 ERROR HttpErrorResponse {headers: HttpHeaders, status: 0, statusText: "Unknown Error", url: null, ok: false, …}

我知道这是一个 CORS 问题,但我找不到任何解决方案。我该怎么做才能到达我的 HTTP 获取请求并收到答复?

【问题讨论】:

  • 正如您正确推断的那样,这与 CORS 有关。在发出跨端请求之前,您必须正确实现由合规客户端发送的 OPTIONS 方法,或者使用不发送请求的客户端。
  • 如何实现 OPTIONS 方法?服务器应该发回什么?
  • 此时您必须参考您最喜欢的 RFC。在实践中,c++ 是一种非常糟糕的语言来创建一个自托管的 Web 服务器,而且它通常很多。用另一种语言简单地从 Web 服务器调用 c++ 代码要容易得多。

标签: c++ http server cors angular-httpclient


【解决方案1】:

HTTP specification 需要 \r\n 而不是 \n

【讨论】:

  • 我在编辑中添加了\r\n,但它不起作用。我仍然遇到同样的错误。
【解决方案2】:

看看https://github.com/Microsoft/cpprestsdk,轻松创建c++服务器。

关于你使用的代码,你可能需要修改函数

void handle_http_request(int fd)

处理 OPTIONS 动词可能是这样的

else if (strcmp(request_type, "OPTIONS") == 0) {
    if (strcmp(request_path, "/d20") == 0) {
          options_d20(fd, body);

        } else {
          resp_404(fd, request_path);
    }
}

然后实现新的 options_d20 函数,这可能需要您添加到标头缓冲区,这些标头:

"Access-Control-Allow-Origin":"*"

"Access-Control-Allow-Methods":"GET, OPTIONS"

"Access-Control-Max-Age":"3600"

"Access-Control-Allow-Headers":"Content-Type, Access-Control-Allow-Headers,Authorization, X-Requested-With"

但我只是猜测。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-01-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-03-16
    • 2014-04-22
    • 2021-10-17
    相关资源
    最近更新 更多