//post
#include<stdio.h>
#include<winsock2.h>
#include<windows.h>
#include<iostream>
#include<fstream>
#pragma comment(lib, "ws2_32.lib")
#define name "www.baidu.com"
#define host "baidu.com"
#define wz "http://baidu.com"
#define max 20480
using namespace std;
void fun(char rcvBuf[]);
int main()
{
int num,sum;
SOCKET s;
WSADATA wsa;
struct sockaddr_in serv;
char sndBuf[1024], rcvBuf[max];
WSAStartup(MAKEWORD(2, 1), &wsa);
struct hostent* host_addr = gethostbyname(name);
if (host_addr == NULL)
{
cout<<"Unable to locate host"<<endl;
return -103;
}
if ((s = socket(AF_INET, SOCK_STREAM, 0))<0)
{
perror("socket error!");
exit(1);
}
memset(&serv,0,sizeof(serv));
serv.sin_family = AF_INET;
serv.sin_port = htons(80);
serv.sin_addr.s_addr = *((int*)*host_addr->h_addr_list);
//serv.sin_addr.S_un.S_addr = inet_addr("115.239.210.26");
if ((connect(s, (struct sockaddr *)&serv, sizeof(serv)))<0)
{
perror("connet error!");
exit(1);
}
memset(sndBuf, 0, 1024);
memset(rcvBuf, 0, max);
//头信息
strcat(sndBuf, "POST ");
strcat(sndBuf, wz);
strcat(sndBuf, " HTTP/1.0\r\n");
strcat(sndBuf, "Host: ");
strcat(sndBuf, host);
strcat(sndBuf, "\r\n");
strcat(sndBuf, "Connection: keep-alive\r\n");
strcat(sndBuf, "\r\n");
puts(sndBuf);
if ((num = send(s,sndBuf,1024, 0))<0)
{
perror("send error!");
exit(1);
}
puts("send success!\n");
do{
sum = recv(s, rcvBuf, max-1, 0);//如果rcvBuf的存储空间减小,则分几次copy
if (sum<0)
{
perror("recv error!");
system("pause");
exit(1);
}
else if (sum>0)
{
cout<<rcvBuf<<endl;
fun(rcvBuf);//2014.11.04
memset(rcvBuf, 0, max);
}
} while (sum>0);
puts("\nread success!\n");
closesocket(s);
system("pause");
WSACleanup();
return 0;
}
void fun(char rcvBuf[])
{
int i=0;
ofstream out_sort;
out_sort.open("F:\\new\\wz.txt",ios::out||ios::app);
if(out_sort.fail())
{
cout<<"打开文件失败!"<<endl;
exit(1);
}
while(rcvBuf[i]!='\0'&&i<max)
{
if(rcvBuf[i]=='h'&&rcvBuf[i+1]=='r'&&rcvBuf[i+2]=='e'&&rcvBuf[i+3]=='f'&&rcvBuf[i+4]=='='&&rcvBuf[i+5]=='"')
{
i+=6;
while(rcvBuf[i]!='"')
{
out_sort<<rcvBuf[i];
i++;
if(i==max)
break;
}
out_sort<<"\n";
}
i++;
}
out_sort.close();
}
套接字与web服务器连接状态信息:
获取的网页信息:
获取的超链接地址: