- 论坛徽章:
- 0
|
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/socket.h>
#include <netdb.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <error.h>
#include <errno.h>
extern int h_errno;
int main(void)
{
int sockfd;
int readn;
char http_head[1000];
struct sockaddr_in server;
struct hostent *h;
if((h=gethostbyname("hi.baidu.com"))==NULL)
{
fprintf(stderr,"gethostbyname error ");
exit(1);
}
bzero(&server,sizeof(server));
server.sin_family=AF_INET;
server.sin_port=htons(80);
memcpy(&server.sin_addr,h->h_addr,h->h_length);
if((sockfd=socket(AF_INET,SOCK_STREAM,0))<0)
{
fprintf(stderr,"socket error %s\n",strerror(errno));
exit(1);
}
if((connect(sockfd,(struct sockaddr*)&server,sizeof(struct sockaddr_in)))<0)
{
fprintf(stderr,"connect error %s\n",strerror(errno));
exit(1);
}
sprintf(http_head,"GET /phps/rss HTTP/1.1\r\n"
"Host: hi.baidu.com\r\n"
"User-Agent: Mozilla/5.0 (X11; U; Linux i686; zh-CN; rv:1.8.1.6) Gecko/20070723 Iceweasel/2.0.0.6 (Debian-2.0.0.6-0etch1)\r\n"
"Accept: text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5\r\n"
"Accept-Language: zh-cn,zh;q=0.5\r\n"
"Accept-Charset: gb2312,utf-8;q=0.7,*;q=0.7\r\n"
"Connection: Close\r\n\r\n");
write(sockfd,http_head,strlen(http_head));
memset(http_head,'\0',1000);
while(readn=read(sockfd,http_head,1000))
{
printf("%s",http_head);
//printf("%d\n",readn);
memset(http_head,'\0',1000);
}
} |
|