纯C++实现的HTTP请求(POST/GET)
来源:互联网 发布:阿里云解析的作用 编辑:程序博客网 时间:2024/05/23 15:29
纯C++实现的HTTP请求(POST/GET),支持windows和linux,
进行简单的封装, 方便调用。实现如下:
#include "HttpConnect.h"#ifdef WIN32#pragma comment(lib,"ws2_32.lib")#endifHttpConnect::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");
原文地址:http://www.cnblogs.com/lidabo/p/6404533.html
阅读全文
0 0
- 纯C++实现的HTTP请求封装(POST/GET)
- 纯C++实现的HTTP请求(POST/GET)
- 纯java版本的http get和post请求
- http的get、post请求实现方法
- Java实现http(post、get)请求
- 【C#】HTTP请求GET,POST
- [C#] HTTP请求GET,POST
- 【C#】HTTP请求GET,POST
- linux c socket 实现http get/post请求
- linux C 实现HTTP get 及post 请求
- Http的post ,get请求
- http get post的请求
- http的GET、POSt请求
- http的get、post请求
- http的Get/post请求
- C语言实现HTTP的GET请求
- curl实现HTTP的GET POST方法的请求
- c++实现http的get和post请求
- 463. Island Perimeter
- 彻底理解ThreadLocal
- 170613 matlab原始信号中加入高斯白噪声的分析
- 安装mysql,1067错误解决办法
- 什么是soap
- 纯C++实现的HTTP请求(POST/GET)
- sql优化
- 一个不成熟的模板引擎思路
- zookeeper配置文件详解
- HBuilder webApp开发(十七)百度地图URI API的使用
- 详解树链剖分
- 以当前时间命名日志文件
- weblogic权限不足解决方案
- 单例