纯C++实现的HTTP请求封装(POST/GET)

简介: 纯C++实现的HTTP请求(POST/GET),支持windows和linux, 进行简单的封装, 方便调用。实现如下: #include "HttpConnect.h" #ifdef WIN32 #pragma comment(lib,"ws2_32.

纯C++实现的HTTP请求(POST/GET),支持windows和linux, 
进行简单的封装, 方便调用。实现如下:

#include "HttpConnect.h"

#ifdef WIN32
#pragma comment(lib,"ws2_32.lib")
#endif

HttpConnect::HttpConnect()
{
#ifdef WIN32 //此处一定要初始化一下,否则gethostbyname返回一直为空 WSADATA wsa = { 0 }; WSAStartup(MAKEWORD(2, 2), &wsa); #endif } HttpConnect::~HttpConnect() { } void HttpConnect::socketHttp(std::string host, std::string request) { int sockfd; struct sockaddr_in address; struct hostent *server; sockfd = socket(AF_INET,SOCK_STREAM,0); address.sin_family = AF_INET; address.sin_port = htons(80); server = gethostbyname(host.c_str()); memcpy((char *)&address.sin_addr.s_addr,(char*)server->h_addr, server->h_length); if(-1 == connect(sockfd,(struct sockaddr *)&address,sizeof(address))){ DBG <<"connection error!"<<std::endl; return; } DBG << request << std::endl; #ifdef WIN32 send(sockfd, request.c_str(),request.size(),0); #else write(sockfd,request.c_str(),request.size()); #endif char buf[1024*1024] = {0}; int offset = 0; int rc; #ifdef WIN32 while(rc = recv(sockfd, buf+offset, 1024,0)) #else while(rc = read(sockfd, buf+offset, 1024)) #endif { offset += rc; } #ifdef WIN32 closesocket(sockfd); #else close(sockfd); #endif buf[offset] = 0; DBG << buf << std::endl; } void HttpConnect::postData(std::string host, std::string path, std::string post_content) { //POST请求方式 std::stringstream stream; stream << "POST " << path; stream << " HTTP/1.0\r\n"; stream << "Host: "<< host << "\r\n"; stream << "User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; zh-CN; rv:1.9.2.3) Gecko/20100401 Firefox/3.6.3\r\n"; stream << "Content-Type:application/x-www-form-urlencoded\r\n"; stream << "Content-Length:" << post_content.length()<<"\r\n"; stream << "Connection:close\r\n\r\n"; stream << post_content.c_str(); socketHttp(host, stream.str()); } void HttpConnect::getData(std::string host, std::string path, std::string get_content) { //GET请求方式 std::stringstream stream; stream << "GET " << path << "?" << get_content; stream << " HTTP/1.0\r\n"; stream << "Host: " << host << "\r\n"; stream <<"User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; zh-CN; rv:1.9.2.3) Gecko/20100401 Firefox/3.6.3\r\n"; stream <<"Connection:close\r\n\r\n"; socketHttp(host, stream.str()); }

 

调用方法:

    HttpConnect *http = new HttpConnect();
    http->getData("127.0.0.1", "/login", "id=liukang&pw=123"); http->postData("127.0.0.1", "/login","id=liukang&pw=123"); 

 

 

 
目录
相关文章
|
3天前
|
API Apache Android开发
对于Android的http请求的容错管理
对于Android的http请求的容错管理
|
4天前
|
JSON 数据格式 Python
Python 的 requests 库是一个强大的 HTTP 客户端库,用于发送各种类型的 HTTP 请求
`requests` 库是 Python 中用于HTTP请求的强大工具。要开始使用,需通过 `pip install requests` 进行安装。发送GET请求可使用 `requests.get(url)`,而POST请求则需结合 `json.dumps(data)` 以JSON格式发送数据。PUT和DELETE请求类似,分别调用 `requests.put()` 和 `requests.delete()`。
14 2
|
5天前
|
API
http代理ip请求并发数是什么?有什么用?
HTTP代理IP请求并发数指单个客户端对API或代理IP同时发起的请求数量,分为API链接请求并发和IP最大连接数。并发是瞬时同时请求,不同提供商限制不同。高并发请求的代理IP服务商能更好地应对程序压力。选择时应考虑这一因素。
|
5天前
|
Go
深度探讨 Golang 中并发发送 HTTP 请求的最佳技术
深度探讨 Golang 中并发发送 HTTP 请求的最佳技术
|
6天前
|
API UED Python
使用Python进行异步HTTP请求的实践指南
使用Python进行异步HTTP请求的实践指南
19 4
|
Web App开发 JavaScript 前端开发
利用XMLHttpRequest 通过HTTP POST向ABAP backend发送数据
Created by Jerry Wang, last modified on Aug 19, 2014 Javascript source code:
159 0
利用XMLHttpRequest 通过HTTP POST向ABAP backend发送数据
|
Web App开发
利用XMLHttpRequest 通过HTTP POST向ABAP backend发送数据
Created by Jerry Wang, last modified on Aug 19, 2014
110 0
利用XMLHttpRequest 通过HTTP POST向ABAP backend发送数据
|
Web App开发 JavaScript 前端开发
利用XMLHttpRequest 通过HTTP POST向ABAP backend发送数据
利用XMLHttpRequest 通过HTTP POST向ABAP backend发送数据
利用XMLHttpRequest 通过HTTP POST向ABAP backend发送数据
|
2月前
|
前端开发
webpack如何设置devServer启动项目为https协议
webpack如何设置devServer启动项目为https协议
186 0
|
7天前
|
存储 算法 安全
[计算机网络]---Https协议
[计算机网络]---Https协议