#pragma once
#include <Windows.h>
#include <winsock.h>
#include <sstream>
#include <iostream>
#pragma comment(lib,"ws2_32.lib")



class WinHttp
{
public:
	WinHttp()
	{
		//此处一定要初始化一下,否则gethostbyname返回一直为空;
		WSADATA wsa = { 0 };
		if (WSAStartup(MAKEWORD(2, 2), &wsa) != 0)
		{
			std::cout << "init WSADATA failed!" << std::endl;
		}
	}

	~WinHttp(){}

public:
	void PostData(std::string host, int port, 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-Type:application/json\r\n";
		stream << "Content-Length:" << post_content.length() << "\r\n";
		stream << "Connection:close\r\n\r\n";
		stream << post_content.c_str();

		Http(host, port, stream.str());
	}

	void GetData(std::string host, int port, 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";

		Http(host, port, stream.str());
	}

private:
	void Http(std::string host, int port, std::string request)
	{
		int sockfd;
		struct sockaddr_in address;
		sockfd = socket(AF_INET, SOCK_STREAM, 0);
		address.sin_family = AF_INET;
		address.sin_port = htons(port);
		address.sin_addr.S_un.S_addr = inet_addr(host.c_str()); //直接填写地址;
		/*
		struct hostent *server = gethostbyaddr(host.c_str(), host.size(), AF_INET);
		//struct hostent *server = gethostbyname(host.c_str());
		if (!server)
		{
			switch (h_errno)
			{
			case HOST_NOT_FOUND:
				fputs("The host was not found.\n", stderr);
				break;
			case NO_ADDRESS:
				fputs("The name is valid but it has no address.\n", stderr);
				break;
			case NO_RECOVERY:
				fputs("A non-recoverable name server error occurred.\n", stderr);
				break;
			case TRY_AGAIN:
				fputs("The name server is temporarily unavailable.", stderr);
				break;
			default:
				break;
			}
			std::cout << "gethostbyaddr(...) failed!" << std::endl;
			return;
		}
		memcpy((char *)&address.sin_addr.s_addr, (char*)server->h_addr, server->h_length);
		*/
		if (-1 == connect(sockfd, (sockaddr *)&address, sizeof(address)))
		{
			std::cout << host << ":" << port << " connection failed!" << std::endl;
			return;
		}

		//std::cout << request << std::endl;

		send(sockfd, request.c_str(), request.size(), 0);
		char buf[1024 * 2] = { 0 }; //不能设置过大,否则会栈溢出,如果必须设置较大,用堆new;
		//char *buf = new char(1024 * 1024);
		int offset = 0;
		int rc;

		while (rc = recv(sockfd, buf + offset, 1024, 0))
		{
			offset += rc;
		}

		buf[offset] = 0; //最后结束符;

		closesocket(sockfd); //断开;

		std::cout << "recv: " << buf << std::endl;

		//delete[] buf;
	}

	inline char* UnicodeToUtf8(const wchar_t* unicode)
	{ // Unicode 转 UTF-8
		int len;
		len = WideCharToMultiByte(CP_UTF8, 0, unicode, -1, NULL, 0, NULL, NULL);
		char *szUtf8 = (char*)malloc(len + 1);
		memset(szUtf8, 0, len + 1);
		WideCharToMultiByte(CP_UTF8, 0, unicode, -1, szUtf8, len, NULL, NULL);
		return szUtf8;
	}
};

使用方法如下:


WinHttp *http = new WinHttp();
http->getData("127.0.0.1", 8080, "/login", "id=admin&pw=123");
http->postData("127.0.0.1", 8080, "/login","id=admin&pw=123");

当然,前题要有Web后台; 

05-04 07:48