没有外部库的C ++中的HTTP请求? [英] HTTP Requests in C++ without external libraries?

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

问题描述

因此,此问题以前曾被问过,但所指出的一般答案是使用外部库,例如cURLpp.所以我很好奇,是否可以仅使用C ++ 14起的标准库来完成HTTP请求.这有多难?

So this question has been asked before, but the general answer pointed to was using an external library such as cURLpp. So I was curious as to if HTTP requests could be done using only the standard libraries as of C++14. How difficult would this be?

例如,我想获取一个XML文档并将其存储在要解析的字符串中.要实现这一目标,必须采取什么步骤?

Say for example, I wanted to get an XML document and store it in a string to be parsed. What steps would have to be taken to achieve this?

如果有人好奇,我将以此为学习经验,以更好地了解HTTP请求的工作原理.

If anyone is curious, I'm doing this as a learning experience to better understand how HTTP requests work.

推荐答案

在我看来,您想从头开始在POSIX套接字API上实现HTTP协议.我自己做的,这很有趣.

It sounds like to me that you want to implement the HTTP protocol from scratch on top of the POSIX sockets API. I have done this myself, it was quite fun.

在此处了解有关套接字API的信息: http://en.wikipedia.org/wiki/Berkeley_sockets

Read about the sockets API here: http://en.wikipedia.org/wiki/Berkeley_sockets

如果要在Windows上工作,请参见此处.

If you want to work on Windows, see here.

此注释中张贴的链接提供了一个使用API​​的很好的起点示例,尽管它奇怪地将客户端和服务器都作为串行逻辑包含在同一程序中-这可能允许它绕过某些程序将客户端或服务器实现为独立程序所需的调用(例如等待传入连接).

This link, posted in the comments, provides a pretty good starting-point example for using the API, although it weirdly includes both the client and the server as serial logic within the same program -- which may allow it to bypass some of the calls (such as waiting for incoming connections) required to implement a client or server as a standalone program.

假设您正在用C ++实现HTTP服务器,则可以选择将客户端实现为网页(在您喜欢的浏览器上运行),如以下黑客所示...

Assuming you are implementing an HTTP server in C++, you might choose to implement the client as a web page (running on your favorite browser), as the following hack demonstrates...

<html>
<head>
</head>
<body>
This web page sends the entered text back to the server upon a button press.
The server's response is then displayed in the box below.
This is only a hack for learning purposes, and not a demonstration of best-practices.
<br>
<textarea id="INP">
Hello world!
</textarea>
<br>
<button onclick="return make_request('INP','GET','test1')">GET Request</button>
<button onclick="return make_request('INP','POST','test2')">POST Request</button>
<div id="result" style='border-style:solid;'>?</div>
<script>
function make_request( textID, request_type, webfn )
   {
   var url = "" + "?webfn="+webfn // assumes this page was served by the same server
   if ( request_type != 'POST' ) url += "&value="+document.getElementById(textID).value;
   var req = new XMLHttpRequest();
   req.open( request_type, url, /*async*/false );
   req.send( request_type=='POST' ? document.getElementById(textID).value : null );
   if ( req.readyState == 4/*complete*/ && req.status == 200/*OK*/ )
      {
      result = req.responseXML.documentElement.getElementsByTagName('value')[0].firstChild.data;
      document.getElementById('result').innerHTML = req.responseText;
      }
   else alert("HTTP request failed");
   return false;
   }
</script>
</body>
</html>

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

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