如何在C ++中处理HTTP请求? [英] How to deal with HTTP requests in C++ ?

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

问题描述

Hello all



我在Linux下有一个C ++项目,该程序将通过HTTP请求接收Xml内容。我已经计划通过使用TinyXML-2库进行XML内容解析,我一直在关注如何接收HTTP请求。关于如何处理发送到我的程序的HTTP请求的任何建议?



更新:

我收到的HTTP请求是内容-type:text / xml。

Hello all

I have a C++ project under Linux, the program will receive Xml content through HTTP requests. I have already planned for XML content parsing by using the TinyXML-2 library, I'm stuck at the point on how to receive HTTP requests. Any advice on how to handle HTTP requests that are sent to my program ?

Update:
The HTTP request i'm receiving is content-type: text/xml.

推荐答案

作为一个特殊目的的HTTP服务器的一个好处是你不必处理整个协议,你可以只是处理你想要的位并发送错误信息几乎任何其他东西。虽然lbcurl可以帮助它主要用于HTTP客户端,而不是服务器,所以我不打扰它。另一种选择可能是编写一个Apache处理程序来做你想要的但是与简单的服务器相比这太过分了。



所以听80端口,等待连接和那么你将从你的客户端获得一块数据。破解标题(它们是数据块开头的文本块),处理您需要的文本块(内容长度和mime类型),将其余部分放入缓冲区并让tinyXML执行其操作。最后发送一个200 OK的正文或4xx响应,描述你的客户发生了什么。



抓取一份 HTTP:权威指南来自亚马逊或您当地的图书馆,您将找到所有您需要知道的内容,然后再了解更多内容。
One good thing about being a special purpose HTTP server is that you don't have to process the entire protocol and you can just handle the bits you want and send an error message for just about anything else. While lbcurl can help it's mostly for HTTP clients, not servers so I wouldn't bother with it. Another option might be to write an Apache handler to do what you want but that's overkill compared to a simple server.

So listen on port 80, wait for a connection and then you'll get a lump of data from your client. Hack off the headers (they're the block of text at the start of the lump of data), process the ones you need (content length and mime type), chuck the rest in a buffer and let tinyXML do its stuff. Finally send a 200 OK with a body or a 4xx response describing what happened to your client.

Grab a copy of HTTP: The Definitive Guide from Amazon or your local library and you'll find out all you need to know and then some more.




有很多搜索结果在Google上搜索c ++处理http请求。

您可以先查看这个 [ ^ ]。

还有其他开源库 [ ^ ]这可以帮助你完成这项任务。



更多libcurl例子是此处。 [ ^ ]



希望这有帮助!

there were many search results on searching "c++ handling http requests" on Google.
You can start by going through this one[^].
There are other open source libraries[^] which can help you with this task.

More libcurl examples are here.[^]

hope this helps!


对于遇到此问题的任何人,请看:



捕获传入的HTTP请求Web服务器 [ ^ ]
For anyone facing this problem, see this:

Capture Incoming HTTP Requests to the Web Server[^]


这篇关于如何在C ++中处理HTTP请求?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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