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
关注
打赏
最近更新
- 深拷贝和浅拷贝的区别(重点)
- 【Vue】走进Vue框架世界
- 【云服务器】项目部署—搭建网站—vue电商后台管理系统
- 【React介绍】 一文带你深入React
- 【React】React组件实例的三大属性之state,props,refs(你学废了吗)
- 【脚手架VueCLI】从零开始,创建一个VUE项目
- 【React】深入理解React组件生命周期----图文详解(含代码)
- 【React】DOM的Diffing算法是什么?以及DOM中key的作用----经典面试题
- 【React】1_使用React脚手架创建项目步骤--------详解(含项目结构说明)
- 【React】2_如何使用react脚手架写一个简单的页面?