C/C++知识点之纯C++实现的HTTP请求封装(POST/GET)
小标 2018-12-03 来源 : 阅读 4000 评论 0

摘要:本文主要向大家介绍了 C/C++知识点之纯C++实现的HTTP请求封装(POST/GET),通过具体的内容向大家展示,希望对大家学习C/C++知识点有所帮助。

本文主要向大家介绍了 C/C++知识点之纯C++实现的HTTP请求封装(POST/GET),通过具体的内容向大家展示,希望对大家学习C/C++知识点有所帮助。

纯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");

本文由职坐标整理并发布,希望对同学们有所帮助。了解更多详情请关注职坐标编程语言C/C+频道!

本文由 @小标 发布于职坐标。未经许可,禁止转载。
喜欢 | 1 不喜欢 | 2
看完这篇文章有何感觉?已经有3人表态,33%的人喜欢 快给朋友分享吧~
评论(0)
后参与评论

您输入的评论内容中包含违禁敏感词

我知道了

助您圆梦职场 匹配合适岗位
验证码手机号,获得海同独家IT培训资料
选择就业方向:
人工智能物联网
大数据开发/分析
人工智能Python
Java全栈开发
WEB前端+H5

请输入正确的手机号码

请输入正确的验证码

获取验证码

您今天的短信下发次数太多了,明天再试试吧!

提交

我们会在第一时间安排职业规划师联系您!

您也可以联系我们的职业规划师咨询:

小职老师的微信号:z_zhizuobiao
小职老师的微信号:z_zhizuobiao

版权所有 职坐标-一站式IT培训就业服务领导者 沪ICP备13042190号-4
上海海同信息科技有限公司 Copyright ©2015 www.zhizuobiao.com,All Rights Reserved.
 沪公网安备 31011502005948号    

©2015 www.zhizuobiao.com All Rights Reserved

208小时内训课程