[Other notes] C ++ under Windows network requests

Work using C ++ access a hardware platform on Window System communication library, can only use C ++ to call them to the dll function, and the communication but also to report the data to the backend server, so tidied C ++ under Windows system The method of network request.

 


 

The first part: knowledge

Used C ++ basics of points

1.C ++ related

  • Class definition
  • Function definition
  • The constructor and destructor
  • #ifdef macros use

2.HTTP related

  • http header
  • Message format of the difference between GET and POST

 


 

Part II: Code

Paste C ++ implementation of a network request code, in fact, the essence is to use Http format messages Socket occur.

1. function claims

  • HttpConnect.h
  • class HttpConnect {
        public:
            void socketHttp(std::string host, std::string request);
            
            void postData(std::string host, std::string path, std::string post_content);
            
            void getData(std::string host, std::string path, std::string get_content);
            
            HttpConnect();
            
            ~HttpConnect();
    };
                

2. The functions implemented

  • HttpConnect.cpp
  • WIN32 #ifdef 
    #pragma the Comment (lib, "ws2_32.lib") 
    #endif 
    #include 
    #include 
    #include 
    
    #include "HttpConnect.h" 
    
    HttpConnect :: HttpConnect () 
    { 
    #ifdef WIN32 
        // here you must initialize it, or else gethostbyname been returned empty 
        WSADATA WSA = {0}; 
        WSAStartup (MAKEWORD (2, 2), & WSA); 
    #endif 
    } 
    
    HttpConnect :: ~ HttpConnect () 
    { 
    
    } 
    void HttpConnect :: socketHttp (STD :: String Host, STD: : Request String) 
    { 
        int sockfd; 
        struct the sockaddr_in address; 
        struct the hostent * Server; 
    
        sockfd = Socket (AF_INET, SOCK_STREAM, 0);  
        address.sin_family = AF_INET;
        address.sin_port = the htons (8091);
        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))){
            std::cout <<"connection error!"<<std::endl;
            return;
        }
    
        std::cout << 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;
        std::cout << 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());
    }
                

Guess you like

Origin www.cnblogs.com/nicojerry/p/11973800.html