LinuxSir.cn,穿越时空的Linuxsir!

 找回密码
 注册
搜索
热搜: shell linux mysql
查看: 1236|回复: 13

懂HTTP的人进来看一下

[复制链接]
发表于 2007-2-18 17:45:03 | 显示全部楼层 |阅读模式
下面是一个发送http请求的程序;

httpclient:

  1. #include <stdarg.h>
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <unistd.h>
  5. #include <arpa/inet.h>
  6. #include <string.h>
  7. #include <sys/socket.h>
  8. #include <netdb.h>

  9. #define MAXBUF  1024

  10. int main(int Count, char *Strings[])
  11. {
  12.   int sockfd, bytes_read, socklen = sizeof(struct sockaddr);
  13.   struct sockaddr_in dest, peer;
  14.   struct hostent * hptr;
  15.   char buffer[MAXBUF];
  16.   char straddr[100];

  17.   /*---Make sure we have the right number of parameters---*/
  18.   if ( Count != 3 )
  19.     printf("usage: httpclient <domain> <page>\n");

  20.   if ( (sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0 )
  21.     {
  22.       perror("socket error");
  23.       abort();
  24.     }

  25.   if((hptr = gethostbyname(Strings[1])) == NULL)
  26.     {
  27.       fprintf(stderr, "gethostbyname error\n");
  28.       exit(1);
  29.     }

  30.   /*---Initialize server address/port struct---*/
  31.   bzero(&dest, sizeof(dest));
  32.   dest.sin_family = AF_INET;
  33.   dest.sin_port = htons(80); /*default HTTP Server port */
  34.   //dest.sin_addr.s_addr = inet_addr(Strings[1]);
  35.   memcpy(&dest.sin_addr, hptr->h_addr, sizeof(struct in_addr));

  36.   /*---Connect to server---*/
  37.   if (connect(sockfd, (struct sockaddr*)&dest, sizeof(dest)) != 0 )
  38.     {
  39.       perror("connect error");
  40.       abort();
  41.     }

  42.   getpeername(sockfd, &peer, &socklen);
  43.   inet_ntop(AF_INET, &peer.sin_addr, straddr, sizeof(straddr));
  44.   printf("peer addr : %s, port : %d\n", straddr, ntohs(peer.sin_port));

  45.   sprintf(buffer, "GET %s HTTP/1.0\r\nCache-Control: no cache\r\n\r\n", Strings[2]);
  46.   //send(sockfd, buffer, strlen(buffer), 0);
  47.   write(sockfd, buffer, strlen(buffer));
  48.   /*---While there's data, read and print it---*/
  49.   do
  50.     {
  51.       bzero(buffer, sizeof(buffer));
  52.       //bytes_read = recv(sockfd, buffer, sizeof(buffer), 0);
  53.       bytes_read = read(sockfd, buffer, sizeof(buffer));
  54.       if ( bytes_read > 0 )
  55.         printf("%s", buffer);
  56.     }
  57.   while ( bytes_read > 0 );

  58.   /*---Clean up---*/
  59.   close(sockfd);
  60.   return 0;
  61. }
复制代码

编译上面的代码:
gcc httpclient.c -o httpclient
然后输入下面的命令
./httpclient news.sina.com.cn /
请求news.sina.com.cn的页面,但是返回的却是:

  1. peer addr : 61.172.201.194, port : 80
  2. HTTP/1.0 403 Forbidden
  3. Server: CachePower/1.3.1.dev
  4. Mime-Version: 1.0
  5. Date: Sun, 18 Feb 2007 09:36:14 GMT
  6. Content-Type: text/html
  7. Content-Length: 1064
  8. Expires: Sun, 18 Feb 2007 09:36:14 GMT
  9. X-Squid-Error: ERR_ACCESS_DENIED 0
  10. X-Cache: MISS from sh-17.sina.com.cn
  11. Connection: close

  12. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
  13. <HTML><HEAD><META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=iso-8859-1">
  14. <TITLE>ERROR: The requested URL could not be retrieved</TITLE>
  15. <STYLE type="text/css"><!--BODY{background-color:#ffffff;font-family:verdana,sans-serif}PRE{font-family:sans-serif}--></STYLE>
  16. </HEAD><BODY>
  17. <H1>ERROR</H1>
  18. <H2>The requested URL could not be retrieved</H2>
  19. <HR noshade size="1px">
  20. <P>
  21. While trying to retrieve the URL:
  22. <A HREF="http://61.172.201.194/">http://61.172.201.194/</A>
  23. <P>
  24. The following error was encountered:
  25. <UL>
  26. <LI>
  27. <STRONG>
  28. Access Denied.
  29. </STRONG>
  30. <P>
  31. Access control configuration prevents your request from
  32. being allowed at this time.  Please contact your service provider if
  33. you feel this is incorrect.
  34. </UL>
  35. <P>Your cache administrator is <A HREF="mailto:webmaster">webmaster</A>.


  36. <BR clear="all">
  37. <HR noshade size="1px">
  38. <ADDRESS>
  39. Generated Sun, 18 Feb 2007 09:36:14 GMT by sh-17.sina.com.cn (CachePower/1.3.1.dev)
  40. </ADDRESS>
  41. </BODY></HTML>
复制代码

有谁知道原因吗?
发表于 2007-2-18 18:33:08 | 显示全部楼层
你试试www.baidu.com行不行?

如果可以的话,就说明sina是虚拟主机,不能直接用ip访问的,要在http头里包含正确的url(比如news.sina.con.cn)才可以
回复 支持 反对

使用道具 举报

发表于 2007-2-18 18:51:28 | 显示全部楼层
yyccrasher is right.
同时要考虑一些常见的HTTP RETURN CODE如HTTP的重定向之类的
另外请用CODE方式上传代码,没有缩进的代码很难看懂
回复 支持 反对

使用道具 举报

 楼主| 发表于 2007-2-18 20:07:34 | 显示全部楼层
./httpclient news.sina.com.cn / 会连接sina主机的ip然后发送:
GET / HTTP/1.0\r\nCache-Control: no cache\r\n\r\n
但是返回的还是上面的Forbidden

./httpclient news.sina.com.cn http://news.sina.com.cn/也试过了,它会发送
GET http://news.sina.com.cn/ HTTP/1.0\r\nCache-Control: no cache\r\n\r\n
但还是Forbidden

应该发送什么样的http请求才可以呢?
回复 支持 反对

使用道具 举报

发表于 2007-2-18 20:32:33 | 显示全部楼层
GET %s HTTP/1.0\r\nCache-Control: no cache\r\n\r\n
就凭这行东西就可以打天下啦??
还是看看RFC的HTTP的协议以后再尝试吧.
回复 支持 反对

使用道具 举报

 楼主| 发表于 2007-2-18 20:46:36 | 显示全部楼层
Post by realtang
GET %s HTTP/1.0\r\nCache-Control: no cache\r\n\r\n
就凭这行东西就可以打天下啦??
还是看看RFC的HTTP的协议以后再尝试吧.

拜托!我这个菜鸟什么时候说过要"打天下"?
我现在只想知道的是我的http请求到底缺少哪一个header field才导致sina发送Forbidden给我的?

希望好心人来帮帮忙
新年快乐
回复 支持 反对

使用道具 举报

发表于 2007-2-18 20:49:17 | 显示全部楼层
http request里的Host字段
回复 支持 反对

使用道具 举报

 楼主| 发表于 2007-2-18 21:47:08 | 显示全部楼层
to Lain:
还是不行,这次我把http请求GET http://news.sina.com.cn/ HTTP/1.1\r\nHost: news.sina.com.cn\r\n\r\n写死在了代码里:
  1. #include <stdarg.h>
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <unistd.h>
  5. #include <arpa/inet.h>
  6. #include <string.h>
  7. #include <sys/socket.h>
  8. #include <netdb.h>
  9. #define MAXBUF  1024
  10. int main(int Count, char *Strings[]) {
  11.         int sockfd, bytes_read;
  12.         struct sockaddr_in dest;
  13.         struct hostent * hptr;
  14.         char buffer[MAXBUF];
  15.         if ( (sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0 ) {
  16.                 perror("socket");
  17.                 exit(1);
  18.         }
  19.         if((hptr = gethostbyname("news.sina.com.cn")) == NULL) {
  20.                 fprintf(stderr, "gethostbyname error\n");
  21.                 exit(1);
  22.         }
  23.         /*---Initialize server address/port struct---*/
  24.         bzero(&dest, sizeof(dest));
  25.         dest.sin_family = AF_INET;
  26.         dest.sin_port = htons(80); /*default HTTP Server port */
  27.         memcpy(&dest.sin_addr, hptr->h_addr, sizeof(struct in_addr));
  28.         /*---Connect to server---*/
  29.         if (connect(sockfd, (struct sockaddr*)&dest, sizeof(dest)) != 0 ) {
  30.                 perror("connect");
  31.                 exit(1);
  32.         }
  33.         sprintf(buffer, "GET http://news.sina.com.cn/ HTTP/1.1\r\nHost: news.sina.com.cn\r\n\r\n");
  34.         write(sockfd, buffer, strlen(buffer));
  35.         /*---While there's data, read and print it---*/
  36.         do {
  37.                 bytes_read = read(sockfd, buffer, sizeof(buffer) - 1);
  38.                 *(buffer + bytes_read) = 0;
  39.                 if ( bytes_read > 0 )
  40.                         printf("%s", buffer);
  41.         } while ( bytes_read > 0 );
  42.         /*---Clean up---*/
  43.         close(sockfd);
  44.         return 0;
  45. }
复制代码
回复 支持 反对

使用道具 举报

发表于 2007-2-19 17:52:58 | 显示全部楼层
GET http://news.sina.com.cn/ HTTP/1.1\r\nHost: news.sina.com.cn\r\n\r\n

改为=>

GET / HTTP/1.1\r\nHost: news.sina.com.cn\r\n\r\n

都叫你看http的rfc了, 你老是不听话.
回复 支持 反对

使用道具 举报

发表于 2007-2-19 20:20:29 | 显示全部楼层
可以先用ethereal 之类的软件的来拾取一下用FIREFOX 返问SINA之网络报文
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

快速回复 返回顶部 返回列表