0

我正在制作一个简单的 C Web 服务器,它解析包含和 xml 正文的 POST 请求。我正在尝试发回响应,但我似乎无法正确处理。这是我的代码:

char res[2000];
strcpy(res, "HTTP/1.1 201 OK\nContent-Type: text/html\n");

printf("%s", res);;
char re[4026];
strcpy(re,"<html><body><p>");
strcat(re, result);
strcat(re, "</p></body></html>\n");
printf("\n%s\n", re);

char tm[1000];
sprintf(tm, "Content-Length: ");
char len[4035];
int l= strlen(re);
sprintf(len, "%d\n", l);
printf("\n%s\n", len);
strcat(tm, len);
printf("\n%s\n", tm);

strcat(res, tm);
strcat(res, re);
printf("\n%s", res);
strcat(res, "\0");

send(sock, res, strlen(res),0);

我不知道为什么,但是当我发送时,会收到注释并且我会留在循环中。

以防万一代码有点难以理解,这是我正在生成的字符串:

HTTP/1.1 201 OK
Content-Type: text/html
Content-Length: 37
<html><body><p>139</p></body></html>
4

1 回答 1

2

您需要在标题之后和正文之前再添加一行:

HTTP/1.1 201 OK
Content-Type: text/html
Content-Length: 37

<html><body><p>139</p></body></html>
于 2013-02-23T20:31:31.243 回答