码迷,mamicode.com
首页 > 移动开发 > 详细

【安卓其他】Windows下的C++网络请求

时间:2019-11-23 12:23:00      阅读:100      评论:0      收藏:0      [点我收藏+]

标签:知识   tar   agent   http   order   wsad   ++   其他   报文   

工作使用了C++在Window系统上接入一个硬件平台的通信库,只能使用C++去调用他们给的dll函数,而且通信后还要把数据上报到后台服务器,所以整理了一下C++在Windows系统下网络请求方法。

 


 

第一部分 : 知识点

用到的C++基础知识点

1.C++相关

  • 类定义
  • 函数定义
  • 构造函数与析构函数
  • #ifdef宏的使用

2.HTTP相关

  • http报文头
  • GET与POST的报文格式区别

 


 

第二部分 : 代码

贴上C++实现的网络请求代码,其实本质就是利用Socket发生Http格式报文。

1.功能声明

  • 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.功能实现

  • HttpConnect.cpp
  • #ifdef WIN32
    #pragma comment(lib,"ws2_32.lib")
    #endif
    #include 
    #include 
    #include 
    
    #include "HttpConnect.h"
    
    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(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());
    }
                

【安卓其他】Windows下的C++网络请求

标签:知识   tar   agent   http   order   wsad   ++   其他   报文   

原文地址:https://www.cnblogs.com/nicojerry/p/11916870.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!