如何从C中的HTTP头中提取数据? [英] How to extract data from HTTP header in C?

查看:317
本文介绍了如何从C中的HTTP头中提取数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

今天我问的是如何从C(不是C ++)的 recv()中的接收缓冲区中提取数据部分。

Today I am asking how to extract the data section from the received buffer in my recv() in C (not C++).

我只需要一些建议,我将如何获得

I just need some suggestions, how would I get

HTTP/1.1 200 OK\r\n
Date: Mon, 23 May 2005 22:38:34 GMT\r\n
Server: Apache/1.3.3.7 (Unix) (Red-Hat/Linux)\r\n
Last-Modified: Wed, 08 Jan 2003 23:11:55 GMT\r\n
ETag: "3f80f-1b6-3e1cb03b"\r\n
Content-Type: text/html; charset=UTF-8\r\n
Content-Length: 131\r\n
Connection: close\r\n
\r\n

<html>
<head>
<title>An Example Page</title>
</head>
<body>
  Hello World, this is a very simple HTML document.
</body>
</html>

上述标题的一部分?它存储在我的缓冲区中,我特意只想剖析数据(页面的源代码)。有什么想法?

The part of the above header? It is stored in my buffer, I specifically just want to dissect the data (the source code of the page). Any ideas?

推荐答案

标题以 \\\\\\\ n结尾。如果整个响应在接收缓冲区中并且您在响应结束时放置了'\0',那么您可以使用以下代码来查找数据部分

The header ends with \r\n\r\n. If the whole response is in the receive buffer and you put a '\0' at the end of the response, then you can use the following code to find the start of the data section

char *data = strstr( buffer, "\r\n\r\n" );
if ( data != NULL )
{ 
    data += 4;
    // do something with the data
}

这篇关于如何从C中的HTTP头中提取数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆