Chinaunix首页 | 论坛 | 博客
  • 博客访问: 201476
  • 博文数量: 48
  • 博客积分: 1935
  • 博客等级: 上尉
  • 技术积分: 491
  • 用 户 组: 普通用户
  • 注册时间: 2010-07-29 00:59
文章分类

全部博文(48)

文章存档

2011年(1)

2010年(47)

我的朋友

分类: LINUX

2010-11-01 01:02:54

模拟浏览器获取web服务器网页源代码的程序

//---------------------------------------------------------------------------------------------
//--name: iesource.c                                                  -------------------------------
//-- to get web sourcecode                                        -------------------------------
//----------------------------------------------------------------------------------------------

#i nclude
#i nclude
#i nclude
#i nclude
#i nclude

int port =80;
#define BUF_SIZE   65536
void getip(char *url)
{
 struct sockaddr_in pin;
 struct hostent *nlp_host;
 int sd=0;
 int len=0;
 int i,count=0;
 int recv_start=0,recv_end=0;
 char buf[BUF_SIZE]={0},myurl[100]={0};
 char host[100]={0}, GET[100]={0},header[240]={0};
 char *pHost = 0;

 ///get the host name and the relative address from url name!!!
 strcpy(myurl,url);
 for(pHost = myurl;*pHost !='/'&& *pHost!='\0';++pHost);
 if((int)(pHost-myurl)==strlen(myurl))
  strcpy(GET,"/");
 else
  strcpy(GET,pHost);
 *pHost = '\0';
 strcpy(host,myurl);
 printf("%s \n  %s  \n",host,GET);
  
 ///setting socket param
 if((nlp_host = gethostbyname(host)) == 0)
 {
  printf("error get host\n");
  exit(1);
 }
 
 bzero(&pin,sizeof(pin));
 pin.sin_family = AF_INET;
 pin.sin_addr.s_addr = htonl(INADDR_ANY);
 pin.sin_addr.s_addr = ((struct in_addr *)(nlp_host->h_addr))->s_addr;
 pin.sin_port = htons(port);
 
 if((sd = socket(AF_INET,SOCK_STREAM,0))== -1)
 {
  printf("Error opening socket!!!\n");
  exit(1);
 }
 
 ///together the request info that will be sent to web server
 ///Note: the blank and enter key byte is necessary,please remember!!!

 strcat(header,"GET");
 strcat(header," ");
 strcat(header,GET);
 strcat(header," ");
 strcat(header,"HTTP/1.1\r\n");
 strcat(header,"HOST:");
 strcat(header,host);
 strcat(header,"\r\n");
 strcat(header,"ACCEPT:*/*");
 strcat(header,"\r\nConnection: close\r\n\r\n\r\n");
 
 ///connect to the webserver,send the header,and receive the web sourcecode  
 if(connect(sd,(void *)&pin,sizeof(pin)) == -1)
  printf("error connect to socket\n");
  
 if(send(sd,header,strlen(header),0) == -1)
 {
  printf("error in send \n");
  exit(1);
 }
 
 ///send the message and wait the response!!!
 len=recv(sd,buf,BUF_SIZE,0);
 if(len<0)
  printf("receive data error!!!\n");
 else
  printf("%s",buf);
 close(sd);
  
}


int main()
{
 char url[256];
 printf("http://");
 scanf("%s",url);
 getip(url);
 return 0;
}
///NOTE: test by dotafox on RedHat 9.0 OS with make 3.79 &gcc-2.95.3

///use: gcc -o iesource iesource.c

阅读(2147) | 评论(1) | 转发(0) |
给主人留下些什么吧!~~

chinaunix网友2010-11-01 15:07:56

很好的, 收藏了 推荐一个博客,提供很多免费软件编程电子书下载: http://free-ebooks.appspot.com