|
下面是一个发送http请求的程序;
httpclient:
- #include <stdarg.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include <unistd.h>
- #include <arpa/inet.h>
- #include <string.h>
- #include <sys/socket.h>
- #include <netdb.h>
- #define MAXBUF 1024
- int main(int Count, char *Strings[])
- {
- int sockfd, bytes_read, socklen = sizeof(struct sockaddr);
- struct sockaddr_in dest, peer;
- struct hostent * hptr;
- char buffer[MAXBUF];
- char straddr[100];
- /*---Make sure we have the right number of parameters---*/
- if ( Count != 3 )
- printf("usage: httpclient <domain> <page>\n");
- if ( (sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0 )
- {
- perror("socket error");
- abort();
- }
- if((hptr = gethostbyname(Strings[1])) == NULL)
- {
- fprintf(stderr, "gethostbyname error\n");
- exit(1);
- }
- /*---Initialize server address/port struct---*/
- bzero(&dest, sizeof(dest));
- dest.sin_family = AF_INET;
- dest.sin_port = htons(80); /*default HTTP Server port */
- //dest.sin_addr.s_addr = inet_addr(Strings[1]);
- memcpy(&dest.sin_addr, hptr->h_addr, sizeof(struct in_addr));
- /*---Connect to server---*/
- if (connect(sockfd, (struct sockaddr*)&dest, sizeof(dest)) != 0 )
- {
- perror("connect error");
- abort();
- }
- getpeername(sockfd, &peer, &socklen);
- inet_ntop(AF_INET, &peer.sin_addr, straddr, sizeof(straddr));
- printf("peer addr : %s, port : %d\n", straddr, ntohs(peer.sin_port));
- sprintf(buffer, "GET %s HTTP/1.0\r\nCache-Control: no cache\r\n\r\n", Strings[2]);
- //send(sockfd, buffer, strlen(buffer), 0);
- write(sockfd, buffer, strlen(buffer));
- /*---While there's data, read and print it---*/
- do
- {
- bzero(buffer, sizeof(buffer));
- //bytes_read = recv(sockfd, buffer, sizeof(buffer), 0);
- bytes_read = read(sockfd, buffer, sizeof(buffer));
- if ( bytes_read > 0 )
- printf("%s", buffer);
- }
- while ( bytes_read > 0 );
- /*---Clean up---*/
- close(sockfd);
- return 0;
- }
复制代码
编译上面的代码:
gcc httpclient.c -o httpclient
然后输入下面的命令
./httpclient news.sina.com.cn /
请求news.sina.com.cn的页面,但是返回的却是:
- peer addr : 61.172.201.194, port : 80
- HTTP/1.0 403 Forbidden
- Server: CachePower/1.3.1.dev
- Mime-Version: 1.0
- Date: Sun, 18 Feb 2007 09:36:14 GMT
- Content-Type: text/html
- Content-Length: 1064
- Expires: Sun, 18 Feb 2007 09:36:14 GMT
- X-Squid-Error: ERR_ACCESS_DENIED 0
- X-Cache: MISS from sh-17.sina.com.cn
- Connection: close
- <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
- <HTML><HEAD><META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=iso-8859-1">
- <TITLE>ERROR: The requested URL could not be retrieved</TITLE>
- <STYLE type="text/css"><!--BODY{background-color:#ffffff;font-family:verdana,sans-serif}PRE{font-family:sans-serif}--></STYLE>
- </HEAD><BODY>
- <H1>ERROR</H1>
- <H2>The requested URL could not be retrieved</H2>
- <HR noshade size="1px">
- <P>
- While trying to retrieve the URL:
- <A HREF="http://61.172.201.194/">http://61.172.201.194/</A>
- <P>
- The following error was encountered:
- <UL>
- <LI>
- <STRONG>
- Access Denied.
- </STRONG>
- <P>
- Access control configuration prevents your request from
- being allowed at this time. Please contact your service provider if
- you feel this is incorrect.
- </UL>
- <P>Your cache administrator is <A HREF="mailto:webmaster">webmaster</A>.
- <BR clear="all">
- <HR noshade size="1px">
- <ADDRESS>
- Generated Sun, 18 Feb 2007 09:36:14 GMT by sh-17.sina.com.cn (CachePower/1.3.1.dev)
- </ADDRESS>
- </BODY></HTML>
复制代码
有谁知道原因吗? |
|