我正在为我的班级制作一个HTTP服务器。但是,Firefox没有显示返回的文件。我构建一个响应头并放入我要发送的文件的右侧content-length,然后通过调用send()发送响应头。接下来我再次使用error_response发送文件,为什么Firefox不显示文件?我将在下面发布相关代码。

正如您在代码中看到的那样,我在404错误期间发送cout << error_response << endl;时遇到问题。使用实时http头我可以看到Firefox收到正确的响应头,<=>确实打印了正确的响应。为什么Firefox不显示它?我是否只需要对包含标题和响应数据包的<=>进行一次调用?

// send response headers
    response->Print( buffer, 2000 );
    if ( send( acc_tcp_sock, buffer, sizeof(buffer), 0 ) < 0 ) {
      cerr << "Unable to send response headers to client." << endl;
      return 5;
    }

    // if 200 and GET then send file
    if ( response->Get_code() == 200 && method == "GET" ) {
      // send file
    }

    // close file, if it has been opened
    if ( response->Get_code() == 200 ) {
      fclose( returned_file );
    }
    else if ( method == "GET" ) {
      if ( send( acc_tcp_sock, error_response.c_str(), error_response.length(), 0 ) < 0 ) {
        cerr << "Unable to send data to client." << endl;
        return 6;
      }
      cout << error_response << endl;
    }

    close( acc_tcp_sock ); // close the connection
  }

  return 0;
}
有帮助吗?

解决方案

您是否尝试在同一个发送命令中发送响应标头? 有没有办法可以告诉firefox正在接收什么。您可以尝试使用一些HTTPRequester类编写一个控制台应用程序,该应用程序向您的服务器发送HTTP请求并查看其获得的回报。

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top