首先要添加引用
#include "winsock2.h" //header
#pragma comment (lib, "ws2_32.lib") //lib
#pragma comment (lib, "ws2_32.lib") //lib
调用的代码:
![](https://images.cnblogs.com/OutliningIndicators/ExpandedBlockStart.gif)
BOOL HttpClient(std::string& rest) //用参数接收返回的字符数据
{
std::string errMsg;//错误信息记录
WSADATA wsaData;
SOCKET sock;
SOCKADDR_IN tcpaddr;
hostent * remoteHost;
char host[] = {"192.168.1.1"}; //主机名就直接写了
int Ret,l;
BOOL done;
int port = 80;
int chars = 0;
char buffer[1024];
if( (Ret = WSAStartup(MAKEWORD(1,1), &wsaData) ) != 0 )
{
errMsg+="WSAStartup failed with error "+Ret;
return 1;
}
if( (remoteHost = gethostbyname(host)) == NULL ) //通过主机名获取地址
{
errMsg+="gethostbyname error!\n";
return 1;
}
sock = socket (AF_INET, SOCK_STREAM, 0);
tcpaddr.sin_family = AF_INET;
tcpaddr.sin_port = htons( (unsigned short)port );
tcpaddr.sin_addr.s_addr = *((unsigned long *)*remoteHost->h_addr_list); //转换地址
if( connect(sock, (const sockaddr * )&tcpaddr, sizeof(tcpaddr)) )
{
errMsg+="connect error!";
return 1;
}
//这里是要发送的http头部
SendString(sock,"GET / HTTP/1.1\r\n");
SendString(sock,"Host:192.168.1.1\r\n");
SendString(sock,"Accept: */*\r\n");
SendString(sock,"User-Agent: Mozilla/4.0");
SendString(sock,"(compatible; MSIE 7.00; Windows 98)\r\n");
SendString(sock,"Connection:Keep-Alive\r\n");
SendString(sock,"\r\n");
SendString(sock,"\r\n");//最后要加空行
done = FALSE;
//FILE * fp;
//fp = fopen("1.txt","w");
//打印并保存http响应的头部
std::string str;
while(!done)
{
l = recv(sock,buffer,1,0);//一个字节一个字节的接受HTTP头
str+=*buffer;
if(l<0)
done=TRUE;
switch(*buffer)
{
case '\r':
break;
case '\n'://判断HTTP头是否接受完毕
if(chars==0)
done = TRUE;
chars=0;
break;
default:
chars++;
break;
}
printf("%c",str);
//fputc(buffer[0],fp);
}
//接收正文部分
int sum = 0;
do
{
l = recv(sock,buffer,1024,0);
if( l < 0 )
break;
sum += l;
str.append(buffer,l);
//*(buffer + l) = 0;
//fputs(buffer,fp);
} while( l > 0 );
//这里输出正文部分大小,发现其实和响应消息头部的Content-length大小是一样的
//这样就可以检查是否接受完毕
printf("sum = %d\n",sum);
closesocket(sock);
if( WSACleanup() == SOCKET_ERROR )
{
errMsg+="WSACleanup failed with error "+WSAGetLastError();
}
rest=str;
return 1;
}
void SendString(SOCKET sock,LPCSTR str)
{
send(sock,str,strlen(str),0);
}
{
std::string errMsg;//错误信息记录
WSADATA wsaData;
SOCKET sock;
SOCKADDR_IN tcpaddr;
hostent * remoteHost;
char host[] = {"192.168.1.1"}; //主机名就直接写了
int Ret,l;
BOOL done;
int port = 80;
int chars = 0;
char buffer[1024];
if( (Ret = WSAStartup(MAKEWORD(1,1), &wsaData) ) != 0 )
{
errMsg+="WSAStartup failed with error "+Ret;
return 1;
}
if( (remoteHost = gethostbyname(host)) == NULL ) //通过主机名获取地址
{
errMsg+="gethostbyname error!\n";
return 1;
}
sock = socket (AF_INET, SOCK_STREAM, 0);
tcpaddr.sin_family = AF_INET;
tcpaddr.sin_port = htons( (unsigned short)port );
tcpaddr.sin_addr.s_addr = *((unsigned long *)*remoteHost->h_addr_list); //转换地址
if( connect(sock, (const sockaddr * )&tcpaddr, sizeof(tcpaddr)) )
{
errMsg+="connect error!";
return 1;
}
//这里是要发送的http头部
SendString(sock,"GET / HTTP/1.1\r\n");
SendString(sock,"Host:192.168.1.1\r\n");
SendString(sock,"Accept: */*\r\n");
SendString(sock,"User-Agent: Mozilla/4.0");
SendString(sock,"(compatible; MSIE 7.00; Windows 98)\r\n");
SendString(sock,"Connection:Keep-Alive\r\n");
SendString(sock,"\r\n");
SendString(sock,"\r\n");//最后要加空行
done = FALSE;
//FILE * fp;
//fp = fopen("1.txt","w");
//打印并保存http响应的头部
std::string str;
while(!done)
{
l = recv(sock,buffer,1,0);//一个字节一个字节的接受HTTP头
str+=*buffer;
if(l<0)
done=TRUE;
switch(*buffer)
{
case '\r':
break;
case '\n'://判断HTTP头是否接受完毕
if(chars==0)
done = TRUE;
chars=0;
break;
default:
chars++;
break;
}
printf("%c",str);
//fputc(buffer[0],fp);
}
//接收正文部分
int sum = 0;
do
{
l = recv(sock,buffer,1024,0);
if( l < 0 )
break;
sum += l;
str.append(buffer,l);
//*(buffer + l) = 0;
//fputs(buffer,fp);
} while( l > 0 );
//这里输出正文部分大小,发现其实和响应消息头部的Content-length大小是一样的
//这样就可以检查是否接受完毕
printf("sum = %d\n",sum);
closesocket(sock);
if( WSACleanup() == SOCKET_ERROR )
{
errMsg+="WSACleanup failed with error "+WSAGetLastError();
}
rest=str;
return 1;
}
void SendString(SOCKET sock,LPCSTR str)
{
send(sock,str,strlen(str),0);
}