亚洲av成人无遮挡网站在线观看,少妇性bbb搡bbb爽爽爽,亚洲av日韩精品久久久久久,兔费看少妇性l交大片免费,无码少妇一区二区三区
Chinaunix
標(biāo)題:
socket 讀取http奇怪的多讀了一些0和其它的數(shù)字
[打印本頁(yè)]
作者:
xjtdy888
時(shí)間:
2007-09-17 11:21
標(biāo)題:
socket 讀取http奇怪的多讀了一些0和其它的數(shù)字
#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);
}
}
作者:
jigloo
時(shí)間:
2007-09-17 11:25
http 1.1支持chunked的Transfer-Encoding
具體看RFC
一個(gè)偷懶的辦法就是把請(qǐng)求頭改成HTTP/1.0
作者:
xjtdy888
時(shí)間:
2007-09-17 11:31
呵呵 確實(shí),謝謝了
作者:
jigloo
時(shí)間:
2007-09-17 11:47
再多下嘴,現(xiàn)在web上大多是動(dòng)態(tài)內(nèi)容,使用chunked Transfer-Encoding傳輸,服務(wù)端就可以一邊生成內(nèi)容一邊發(fā)送給客戶端。而HTTP 1.0就不能,所以比較慢。
對(duì)于多個(gè)請(qǐng)求,使用HTTP 1.1還能得到持久連接和流水線的好處,所以推薦使用1.1
當(dāng)然這些功能一般不是自己實(shí)現(xiàn),用libcurl就可以了。
要是不嫌慢的話, perl的LWP和python的urllib2也很棒。
雖然perl/python都提供curl的封裝,用起來(lái)不那么地道就是了。
歡迎光臨 Chinaunix (http://72891.cn/)
Powered by Discuz! X3.2