【安卓其他】Windous C++下的Post、Get请求

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

 

C++知识点: 

1.类定义

2.函数定义

3.构造函数与析构函数

4.#ifdef宏的使用

 

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();
};

 

HttpConnect.cpp

#ifdef WIN32
#pragma comment(lib,"ws2_32.lib")
#endif
#include <iostream>
#include <Winsock2.h>
#include <sstream>

#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());
}

 

转载于:https://www.cnblogs.com/nicojerry/p/11589683.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值