您当前的位置: 首页 >  http

qianbo_insist

暂无认证

  • 0浏览

    0关注

    399博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

httpclient 手写

qianbo_insist 发布时间:2019-08-01 16:17:03 ,浏览量:0

http client c++ 代码 为何手写
### 很多c ,c++ 的获取http协议内容,get或者post 的时候,会犯难,是自己写还是使用现成的库,动辄编译一个库是比较消耗时间的,
能自己手写就不要随便编译库了。
#include 
#include 
#ifdef _WIN32
#include 
#endif
using namespace std;
//获取content-length 的长度,截取
int contentlen_get(char * pos, char * end)
{
	char buf[16];
	char * t = pos;
	while (*(t) == ' ')
		t++;
	char * e = t;
	int i = 0;
	while ((*e) != '\r')
	{
		buf[i++] = *e;
		e++;
		if (i > 14)
			return -1;
	}
	buf[i] = '\0';
	return atoi(buf);
	//return 0;
}

class http_connect
{
public:

	http_connect::http_connect()
	{
#ifdef _WIN32
		//此处一定要初始化一下,否则gethostbyname返回一直为空
		WSADATA wsa = { 0 };
		WSAStartup(MAKEWORD(2, 2), &wsa);
#endif
	}

	http_connect::~http_connect()
	{
		WSACleanup();
	}
	int http_connect::socketHttp(const char* host, unsigned short port, 
		const char *request,char *buf, int buflen)
	{
		SOCKET sockfd;
		struct sockaddr_in address;
		struct hostent *server;

		sockfd = socket(AF_INET, SOCK_STREAM, 0);
		address.sin_family = AF_INET;
		address.sin_port = htons(port);
		//getaddrinfo()
		server = gethostbyname(host);
		memcpy((char *)&address.sin_addr.s_addr, (char*)server->h_addr, server->h_length);

#ifdef _WIN32
		int ret = 0;
		//int timeout = 2000; //2s
		//这样做在Linux环境下是不会产生效果的,须如下定义:
		struct timeval timeout = { 2,0 };
		//设置发送超时
		setsockopt(sockfd, SOL_SOCKET, SO_SNDTIMEO, (char *)&timeout, sizeof(struct timeval));
		//设置接收超时
		setsockopt(sockfd, SOL_SOCKET, SO_RCVTIMEO, (char *)&timeout, sizeof(struct timeval));


		//		ret = setsockopt(sockfd, SOL_SOCKET, SO_SNDTIMEO, (const char*)&timeout, sizeof(timeout));
		//		ret = setsockopt(sockfd, SOL_SOCKET, SO_RCVTIMEO, (const char*)&timeout, sizeof(timeout));
#endif
		if (-1 == connect(sockfd, (struct sockaddr *)&address, sizeof(address))) {
#ifdef _WIN32 
			closesocket(sockfd);
#endif
			printf("connection error!\n");
			return -1;
		}



#ifdef WIN32
		int iRes = send(sockfd, request, (int)strlen(request), 0);
		if (iRes == SOCKET_ERROR) {
			printf("send failed: %d\n", WSAGetLastError());
			closesocket(sockfd);
			return -1;
		}
#else
		struct timeval timeout = { 2,0 };//2s
		int ret = setsockopt(sock_fd, SOL_SOCKET, SO_SNDTIMEO, (const char*)&timeout, sizeof(timeout));
		int ret = setsockopt(sock_fd, SOL_SOCKET, SO_RCVTIMEO, (const char*)&timeout, sizeof(timeout));
		write(sockfd, request, strlen(request));
#endif
		//char * buf = new char[64 * 1024];
		int offset = 0;
		int rc = 0;
		do {
			buflen = buflen - offset;
			if (buflen > 0)
			{
#ifdef WIN32
				rc = recv(sockfd, buf + offset, buflen, MSG_WAITALL);
#else
				rc = read(sockfd, buf + offset, 1024);
#endif
			}
			if (rc > 0) {
				offset += rc;
				printf("Bytes received: %d\n", rc);
			}
			else if (rc == 0)
			{
				printf("Connection closed\n");
			}
			else
			{
				printf("recv failed: %d\n", rc);
			}
		} while (rc > 0);

#ifdef WIN32
		closesocket(sockfd);
#else
		close(sockfd);
#endif
		buf[offset] = 0;
		const char * content_length = strstr(buf, "Content-Length:");
		const char * pos = strstr(buf, "\r\n\r\n");
		if (content_length == NULL  || pos == NULL)
		{
			return -1;
		}
		else
		{
			content_length += 15;
			int len = contentlen_get(" 8\r\n", NULL);
			if (len             
关注
打赏
1663161521
查看更多评论
0.0441s