Created
November 24, 2011 03:41
-
-
Save qianguozheng/1390582 to your computer and use it in GitHub Desktop.
Get WebPage Title Using C language
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <string.h> | |
#include <sys/socket.h> | |
#include <netinet/in.h> | |
#include <arpa/inet.h> | |
#include <netdb.h> | |
#include <unistd.h> | |
char * getWebPageTitle(char * website) | |
{ | |
char myurl[BUFSIZ]; | |
int sockfd; | |
struct sockaddr_in addr; | |
struct hostent *pURL; | |
char *pHost,*q; | |
char host[BUFSIZ],GET[BUFSIZ],header[BUFSIZ]; | |
char buffer[BUFSIZ]; | |
//process the website | |
strcpy(myurl,website); | |
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); | |
printf("socket\n"); | |
if((sockfd=socket(AF_INET,SOCK_STREAM,0))==-1) | |
{ | |
printf("create socket error!\n"); | |
} | |
printf("gethostbyname\n"); | |
if((pURL=gethostbyname(host))==NULL) | |
{ | |
printf("cannot get host name!\n"); | |
return 1; | |
} | |
memset(buffer,0,BUFSIZ); | |
memset(&addr,0,sizeof(struct sockaddr_in)); | |
addr.sin_family=AF_INET; | |
addr.sin_port=htons(80); | |
addr.sin_addr.s_addr=*((unsigned long *)pURL->h_addr); | |
printf("connect\n"); | |
if(connect(sockfd,(struct sockaddr *)&addr,sizeof(addr))) | |
{ | |
printf("cannot connect to server\n"); | |
close(sockfd); | |
return 1; | |
} | |
//make the http request | |
strcat(header,"GET "); | |
strcat(header,GET); | |
strcat(header," HTTP/1.1\r\n"); | |
strcat(header,"Host: "); | |
strcat(header,host); | |
strcat(header,"\r\nConnection:Close\r\n\r\n"); | |
printf("header: %s",header); | |
write(sockfd,header,sizeof(header)); | |
while((read(sockfd,buffer,BUFSIZ))>0) | |
{ | |
if(q=strstr(buffer,"<title>")) | |
{ | |
q+=7; | |
if(strtok(q,"</title>")) | |
{ | |
//printf("%s\n,q"); | |
return q; | |
} | |
} | |
} | |
} | |
int main(int argc,char *argv[]) | |
{ | |
char* title; | |
title=getWebPageTitle("www.yahoo.com"); | |
printf("Title:%s\n",title); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment