简体   繁体   English

HTTP 响应格式不正确?

[英]HTTP Response Format incorrect?

I was looking at creating WebServers with C++.我正在考虑用 C++ 创建 WebServers。
I followed this tutorial by Sloan Kelly, and referenced this answer here.我遵循了 Sloan Kelly 的本教程,并在此处引用了此答案

I wrote (improperly adapted) the following code (showing the sending part only):我写了(不正确地改编)以下代码(仅显示发送部分):

std::stringstream make_response;
make_response << "HTTP/1.1 200 OK\r\n";
make_response << "Cache-Control: no-cache, private\r\n";
make_response << "Content-Type: text/html\r\n";
make_response << "Content-Length: 88\r\n";
make_response << "\r\n";
make_response << "Hello!";
std::string finished_response = make_response.str();

send(client, finished_response.c_str(), finished_response.length()+1, 0);

What is the problem here?这里有什么问题? When I connect to 127.0.0.1:8000 , the screen is entirely blank.当我连接到127.0.0.1:8000 ,屏幕完全空白。

I know that the request reaches me, as I have printed it in the console:我知道请求到达了我,因为我已经在控制台中打印了它:

C:\Users\Jaideep Shekhar\OneDrive\Documents\Projects\ProjectC++\Library\v0.1\main>webserver.exe
Listening for incoming connections...
Client connected!
Client says: GET / HTTP/1.1
Host: 127.0.0.1:8000
Connection: keep-alive
Cache-Control: max-age=0
Upgrade-Insecure-Requests: 1
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/77.0.3865.75 Safari/537.36
Sec-Fetch-Mode: navigate
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3
Sec-Fetch-Site: cross-site
Accept-Encoding: gzip, deflate, br
Accept-Language: en-US,en;q=0.9

@_M
Client disconnected.

You can copy the whole source code from here and open up your terminal:你可以从这里复制整个源代码并打开你的终端:

g++ webserver.cpp -o webserver.exe -lws2_32 && webserver.exe

Note that this code is for Windows OS.请注意,此代码适用于 Windows 操作系统。

Edit : Thanks to a comment, I solved the problem (see code below).编辑:感谢评论,我解决了这个问题(见下面的代码)。 Any improvements or problems in the code are welcome to be pointed out.欢迎指出代码中的任何改进或问题。

Edit2 : How would I send images? Edit2 :我将如何发送图像? I am opening the file opened with std::ios::binary , but it is not being recieved by the browser.我正在打开用std::ios::binary打开的文件,但浏览器没有收到它。

Thanks!谢谢!

Okay, looks like the format and headers were all correct after all.好的,看起来格式和标题毕竟都是正确的。 I made a mistake in responding that I am sending 88 bytes of content, while I sent 5.我在回应时犯了一个错误,我发送了 88 个字节的内容,而我发送了 5 个字节。

In fact, to avoid these issues, I have now put the HTML in its separate file.事实上,为了避免这些问题,我现在将 HTML 放在单独的文件中。

The relevant part looks like this now:相关部分现在看起来像这样:

// Transfer the whole HTML to a std::string
std::fstream grab_content("home.html");
std::stringstream make_content;
make_content << grab_content.rdbuf();
std::string finished_content;
finished_content = make_content.str();

// Create the HTTP Response
std::stringstream make_response;
make_response << "HTTP/1.1 200 OK\r\n";
make_response << "Cache-Control: no-cache, private\r\n";
make_response << "Content-Type: text/html\r\n";
make_response << "Content-Length: " << finished_content.length() << "\r\n";
make_response << "\r\n";
make_response << finished_content;
// Transfer it to a std::string to send
std::string finished_response = make_response.str();

// Send the HTTP Response
send(client, finished_response.c_str(), finished_response.length(), 0);  // flag = 0

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM