如何解析用C HTTP响应? [英] How to parse HTTP responses in C?

查看:164
本文介绍了如何解析用C HTTP响应?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在写一个小项目,与一组使用HTTP 1.1 GET和POST服务器交互。服务器给我一些标题行后的反应,所以我虽然使用的strtok()函数中使用 \\ n 作为分隔符,但有一个崩溃每当我尝试这样做。

有没有简单的方法来分析在C HTTP响应?我想不使用第三方库本,但如果这是真的necesary我不会有任何问题。

非常感谢你的一切。

编辑:下面是一些例子code,只是想打印的行:

 的char *响应=HTTP / 1.1 200 OK \\ n服务器:Apache的狼/ 1.1 \\ nPragma:无缓存字符*记号= NULL;
令牌= strtok的(回应,\\ n);
而(令牌){
    的printf(当前令牌:%S \\ n,令牌);
    令牌= strtok的(NULL,\\ n);
}


解决方案

HTTP分析器是用C语言编写的Node.js的项目中的简单和超快速HTTP解析

这是只有2个C文件,无需任何外部的依赖。

I'm writing a little project which interacts with a set of servers using HTTP 1.1 GET and POST. The server gives me the response after some header lines, so I though on using strtok() function using \n as the delimiter but there is a crash whenever I try to do so.

Is there any simple way to parse a HTTP response in C? I would like not to use 3rd party libraries for this but if it was really necesary I won't have any problem.

Thank you very much for everything.

EDIT: Here is some example code, just trying to print the lines:

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");
}

解决方案

http-parser is a simple and super-fast HTTP parser written in C for the Node.js project

It's only 2 C files, without any external dependencies.

这篇关于如何解析用C HTTP响应?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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