我正在写一个小项目,它使用HTTP 1.1 GET和POST与一组服务器进行交互。服务器给了我一些标题行之后的响应,所以我尽管使用strtok()
函数使用\n
作为分隔符,但每当我尝试这样做时就会崩溃。如何解析C中的HTTP响应?
有没有简单的方法来解析C中的HTTP响应?我不想为此使用第三方库,但如果它真的需要,我不会有任何问题。
非常感谢您的一切。
编辑:下面是一些示例代码,只是想打印线:
char *response = "HTTP/1.1 200 OK\nServer: Apache-Coyote/1.1\nPragma: no-cache"
char *token = NULL;
token = strtok(response, "\n");
while (token) {
printf("Current token: %s.\n", token);
token = strtok(NULL, "\n");
}
请发布您的代码 – 2010-03-07 04:41:47
当在一个线程或多进程程序中使用时,可能需要使用'strtok_r()'来保证安全,如果可用的话,请考虑使用它 – mctylr 2010-03-07 05:07:41
试图修改来自'“...”'字符串文字的字符数组的任何元素都是**未定义的行为**;请参阅http://c0x.coding-guidelines.com/6.4.5.html和http://www.catb.org/jargon/html/N/nasal-demons.html。 – ephemient 2010-03-07 05:47:02