純C++實現的HTTP請求封裝(POST/GET)

來源:互聯網
上載者:User

標籤:comment   否則   str   startup   dig   path   lin   win32   ring   

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

 

 

 

純C++實現的HTTP請求封裝(POST/GET)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.