栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

C++ 实现Get和Post请求(亲测)

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

C++ 实现Get和Post请求(亲测)

废话不多说,直接上代码

//#include 
#include "winsock2.h"
#include 
#include 
using namespace std;


#pragma comment(lib, "ws2_32.lib")
#define IPSTR "127.0.0.1" //服务器IP地址;
#define PORT 8080 //服务器端口;
#define BUFSIZE 1024

class httpClient {
		std::string host;
		std::string path;
		std::string post_content;
	public:
		std::string httpSend(std::string httpHeader);
		std::string httpGet(std::string host, std::string path);
		std::string httpPost(std::string host, std::string path,std::string post_content);
};
std::string httpClient::httpSend(std::string httpHeader) {
	//POST请求方式
	std::string ret = ""; // 返回Http Response
	// 开始进行socket初始化
	WSADATA wData;
	::WSAStartup(MAKEWORD(2, 2), &wData);
	SOCKET clientSocket = socket(AF_INET, 1, 0);
	struct sockaddr_in ServerAddr = {0};
	ServerAddr.sin_addr.s_addr = inet_addr(IPSTR);
	ServerAddr.sin_port = htons(PORT);
	ServerAddr.sin_family = AF_INET;
	int errNo = connect(clientSocket, (sockaddr*)&ServerAddr, sizeof(ServerAddr));
	if(errNo == 0) {
		errNo = send(clientSocket, httpHeader.c_str(), httpHeader.length(), 0);//发送头文件 
		if(errNo > 0)  {
			std::cout << "发送成功" << std::endl;
		} else {
			std::cout << "errNo:" << errNo << std::endl;
			return ret;
		}
		// 接收
		char bufRecv[3069] = {0};
		errNo = recv(clientSocket, bufRecv, 3069, 0);
		if(errNo > 0) {
			ret = bufRecv;// 如果接收成功,则返回接收的数据内容
			char *p;
			char *delims= {"n"} ;
			p=strtok(bufRecv,"n");
			int i = 0;
			while(p!=NULL) {
				if(i == 13){
//					printf("word: %sn",p); //对获取的数据进行分割,获取每一行的数据 
					ret = p;
				}
				i++;
				p=strtok(NULL,delims);
			}
			return ret;
		} else {
			std::cout << "errNo:" << errNo << std::endl;
			return ret;
		}

	} else {
		errNo = WSAGetLastError();
		std::cout << "errNo:" << errNo << std::endl;
	}
	// socket环境清理
	::WSACleanup();
}

std::string httpClient::httpGet(std::string host, std::string path) {
	std::string httpHeader;
	httpHeader=
		"GET "+path+" HTTP/1.1rn"
        "Host: "+host+"rn"
        "User-Agent: IE or Chromern"
        "Accept-Type: *
std::string httpClient::httpPost(std::string host, std::string path,std::string post_content) {
	std::string httpHeader;
	char lenChar[16]; //这个长度根据需要吧
	sprintf(lenChar, "%d", post_content.length());
	std::string len(lenChar);
	httpHeader=
		"POST "+path+" HTTP/1.1rn"
       	"Host: "+host+"n"
       	"Connection: Closern"
       	"Content-Type: application/jsonn"
       	"Content-Length: "+ len +"nn"
       	+post_content;
    return httpSend(httpHeader);
}
int main() {
	httpClient *http = new httpClient();
//	***********************GET*****************************	
	std::cout<<"Get:"<< http->httpGet("127.0.0.1:8080", "/admin") <httpPost("127.0.0.1:8080", "/withBody1",message)< 

C语言版本:
https://blog.csdn.net/weixin_45721882/article/details/120932661

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/346919.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号