美文网首页
通过C++编程发起HTTP请求

通过C++编程发起HTTP请求

作者: StoneHeart | 来源:发表于2016-05-15 14:48 被阅读3125次

前些天完成一个小项目,其主要功能是通过C++编程发起http请求,并对通讯信息加密。其中难点在于对方是java服务器,交换数据信息需要严格按照某种数据交换格式如json,xml等来定义。期间使用了一个第三方库ghttp,下面我们主要来分析ghttp源码来了解整个http请求的过程。

第一步:解析域名通过域名获取对应请求的主机或服务器的ip和端口。
第二步:通过socket编程向上述主机或服务器的ip和端口建立连接。
第三步:把请求内容按照http协议来进行组装
第四步:将封装好的http协议内容发送到目的服务器
第五步:接收服务器反馈内容

htpp协议与链接地址的全部内容_ghttp_request结构体
<pre>
<code>struct _ghttp_request</code>
{
http_uri *uri;

http_uri *proxy;

http_req *req;

http_resp *resp;
http_trans_conn *conn;
const char *errstr;
int connected;
ghttp_proc proc;
char *username;
char *password;
char *authtoken;
char *proxy_username;
char *proxy_password;
char *proxy_authtoken;
};
</pre>

*** 1.在ghttp中与建立连接相关的数据结构和函数定义 ***
1.1 http_uri是对请求URL对象的解析
<pre>
typedef struct http_uri_tag
{
char full; / full URL */
char proto; / protocol */
char host; / copy semantics */
unsigned short port;
char resource; / copy semantics */
} http_uri;
</pre>
1.2 http_trans_conn是对信息发送对象的封装
<pre>typedef struct http_trans_conn_tag {
struct hostent *hostinfo;
struct sockaddr_in saddr;
char *host;
char proxy_host;
int sock;
short port;
short proxy_port;
http_trans_err_type error_type;
int error;
int sync; /
sync or async? */
char io_buf; / buffer /
int io_buf_len; /
how big is it? /
int io_buf_alloc; /
how much is used /
int io_buf_io_done; /
how much have we already moved? /
int io_buf_io_left; /
how much data do we have left? /
int io_buf_chunksize; /
how big should the chunks be that get
read in and out be? /
int last_read; /
the size of the last read /
int chunk_len; /
length of a chunk. */
char errstr; / a hint as to an error */
} http_trans_conn;
</pre>

通过对URL的解析将必要的连接目标信息装入http_uri对象。然后从http_uri对象中取出host和port保存到http_trans_conn对象。http_trans_conn不仅保存目的服务器的IP与端口信息还定义了发送和接收缓存以及同步异步方式。

1.3 信息交换的三大函数
<pre>int http_trans_connect(http_trans_conn *a_conn)</pre>
通过向http_trans_conn指定的host和port发起连接,其中使用gethostbyname(a_conn->host)来获取指定主机的真实ip地址。通过socket方式建立连接。

<pre>int http_req_send(a_request->req, a_request->conn)
</pre>
向连接的目的地发送数据,实现函数write();

<pre>
int http_resp_read_headers(a_request->resp, a_request->conn)
int http_resp_read_body(a_request->resp,a_request->req,a_request->conn)
</pre>
*从连接通道中读取数据,实现函数read();

*** 2 在ghttp中对http协议的封装 ***

2.1 请求报文
<pre>typedef struct http_req_tag {
http_req_type type;
float http_ver;
char *host;
char *full_uri;
char *resource;
char *body;
int body_len;
http_hdr_list *headers;
http_req_state state;
} http_req;
</pre>
下面以post方法为例加上一个标准的请求报文来分析这个结构体。该请求报文的请求方法为post保存在http_req_type type类型的type里。协议版本号HTTP/1.1保存在http_ver里。/search保存在resource属性里面。
<pre>POST /search HTTP/1.1</pre>

host保存到char *host属性中。
<pre>
Host: <a href="http://www.google.cn">www.google.cn</a>
</pre>
下面此段都为头部属性以键值对列的形式存储在
http_hdr_list *headers中。
<pre>
Accept: image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/vnd.ms-excel, application/vnd.ms-powerpoint,
application/msword, application/x-silverlight, application/x-shockwave-flash, /
Referer: <a href="http://www.google.cn/">http://www.google.cn/</a>
Accept-Language: zh-cn
Accept-Encoding: gzip, deflate
User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 2.0.50727; TheWorld)
Connection: Keep-Alive
Cookie: PREF=ID=80a06da87be9ae3c:U=f7167333e2c3b714:NW=1:TM=1261551909:LM=1261551917:S=ybYcq2wpfefs4V9g;
NID=31=ojj8d-IygaEtSxLgaJmqSjVhCspkviJrB6omjamNrSm8lZhKy_yMf
O2M4QMRKcH1g0iQv9u-2hfBW7bUFwVh7pGaRUb0RnHcJU37y-
FxlRugatx63JLv7CWMD6UB_O_r
</pre>
在发送前http_req结构体中报文的各种信息按照http协议标准格式定义来封装,存入http_trans_conn结构体中的io_buf中。然后通过write方法发送。

2.2 响应报文

通过read函数读取接收到的数据保存到http_trans_conn中的io_buf中并由io_buf_alloc指定其在io_buf中的位置。然后通过
<pre>
int http_resp_read_headers(http_resp *a_resp, http_trans_conn *a_conn)
int http_resp_read_body(http_resp *a_resp,http_req *a_req,http_trans_conn *a_conn)
</pre>
俩函数来将io_buf中的内容解析到http_resp结构体中。
<pre>typedef struct http_resp_tag
{
float http_ver;
int status_code;
char *reason_phrase;
http_hdr_list *headers;
char *body;
int body_len;
int content_length;
int flushed_length;
http_resp_header_state header_state;
http_resp_body_state body_state;
} http_resp;
</pre>
通过本文,我们可以看到协议即语境,不管你是哪国人,中国人,美国人或者日本人,只要你使用相同的语境(英语或者其他语言)交流大家都能实现基本的沟通。不同平台也是不管你是C++实现或者java,只要严格按照遵守http协议规定都能实现有效的信息交换。

相关文章

  • 通过C++编程发起HTTP请求

    前些天完成一个小项目,其主要功能是通过C++编程发起http请求,并对通讯信息加密。其中难点在于对方是java服务...

  • 爬虫笔记(1):爬虫原理

    爬虫基本流程: 1) 发起请求: 通过HTTP库向目标站点发起请求,即发送request(请求可以包含额外的hea...

  • 网络爬虫基本原理——基于python语言

    什么是爬虫? 请求网站并提取数据的自动化程序 爬虫基本流程 ▌发起请求 通过HTTP库向目标站点发起请求,即发起一...

  • Day1爬虫原理

    爬虫基本流程 发起请求 通过HTTP库向目标站点发起请求,即发送一个Request,请求可以包含额外的header...

  • 爬虫基本原理

    1. 基本流程 1.1 发起请求 通过http库向目标站点发起请求,即发送一个Request,请求可以包含额外的信...

  • 02、爬虫基本原理

    1、什么是爬虫? 请求网站并提取数据的自动化程序 2、爬虫基本流程 发起请求:通过HTTP库向目标站点发起请求...

  • http、socket、webservice、service、t

    http通过socket实现长连接和短连接,http短连接请求是发起请求获得返回数据后断开socket。socke...

  • python爬虫从入门到放弃2

    一,爬虫的基本流程 发起请求 通过HTTP库向目标站点发起请求,也就是发送一个Request,请求可以包含额外的h...

  • python爬虫教程:爬虫的基本流程 !

    爬虫的基本流程 1.发起请求: 通过HTTP库向目标站点发起请求,即发送一个Request,请求可以包含额外的he...

  • 爬虫基本原理介绍

    什么是爬虫请求网站并提取数据的自动化程序 爬虫基本请求 发起请求通过http库向目标站点发起请求,即发送一个req...

网友评论

      本文标题:通过C++编程发起HTTP请求

      本文链接:https://www.haomeiwen.com/subject/ykzprttx.html