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

前些天完成一个小项目,其主要功能是通过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协议规定都能实现有效的信息交换。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 135,412评论 19 139
  • 第一章 Nginx简介 Nginx是什么 没有听过Nginx?那么一定听过它的“同行”Apache吧!Ngi...
    JokerW阅读 32,872评论 24 1,002
  • https://nodejs.org/api/documentation.html 工具模块 Assert 测试 ...
    KeKeMars阅读 11,509评论 0 6
  • 一、概念(载录于:http://www.cnblogs.com/EricaMIN1987_IT/p/3837436...
    yuantao123434阅读 12,686评论 6 152
  • Http协议详解 标签(空格分隔): Linux 声明:本片文章非原创,内容来源于博客园作者MIN飞翔的HTTP协...
    Sivin阅读 10,607评论 3 82