HTTP request Encapsulation (Post/get) for pure C + + implementations

Source: Internet
Author: User

Pure C + + implementation of HTTP requests (Post/get), supported for Windows and Linux,
Simple encapsulation makes it easy to call. The implementation is as follows:

#include "HttpConnect.h"#ifdef WIN32#pragma comment (lib, "Ws2_32.lib")#endifHttpConnect:: Httpconnect () {#ifdef WIN32Be sure to initialize this here, otherwise gethostbyname returns always null wsadata WSA = {0}; WSAStartup (Makeword (2,2), &AMP;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 *) &AMP;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 ());#endifChar buf[1024*1024] = {0};int offset =0;int RC;#ifdef WIN32while (rc = recv (SOCKFD, Buf+offset,1024,0))#elsewhile (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::p ostdata (STD::String host,STD::String path,STD::String post_content) {Post request modeSTD::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 request mode 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 ());}       

Call Method:

    *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 request Encapsulation (Post/get) for pure C + + implementations

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.