注册 登录  
 加关注
   显示下一条  |  关闭
温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!立即重新绑定新浪微博》  |  关闭

长风明志的博客

不要也不能做下一个谁,应该且可以做第一个自己

 
 
 

日志

 
 

【转】Linux下使用C语言获取一个网页  

2012-10-23 23:01:17|  分类: 搜索引擎 |  标签: |举报 |字号 订阅

  下载LOFTER 我的照片书  |

This C program fetches a web page using the HTTP protocol and Unix sockets. It's based on getaddrinfo and socket.


#include <errno.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <err.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
#include <arpa/inet.h>

/* "BSIZE" is the size of the buffer we use to read from the socket. */

#define BSIZE 0x1000

/* Get the web page and print it to standard output. */

static void get_page (int s, const char * host, const char * page)
{
char * msg;

/* "format" is the format of the HTTP request we send to the web
server. */

const char * format =
"GET /%s HTTP/1.0\r\nHost: %s\r\nUser-Agent: fetch.c\r\n\r\n";
asprintf (& msg, format, page, host);
send (s, msg, strlen (msg), 0);
while (1) {
int bytes;
char buf[BSIZE+10];
bytes = recvfrom (s, buf, BSIZE, 0, 0, 0);
if (bytes == -1) {
fprintf (stderr, "%s\n", strerror(errno));
exit (1);
}
buf[bytes] = '\0';
printf ("%s", buf);
if (bytes == 0) {
break;
}
}
free (msg);
}

int main ()
{
struct addrinfo hints, *res, *res0;
int error;
/* "s" is the file descriptor of the socket. */
int s;
/* Get one of the web pages here. */
const char * host = "www.lemoda.net";

memset (&hints, 0, sizeof(hints));
/* Don't specify what type of internet connection. */
hints.ai_family = PF_UNSPEC;
hints.ai_socktype = SOCK_STREAM;
error = getaddrinfo (host, "http", & hints, & res0);
if (error) {
fprintf (stderr, "%s\n", gai_strerror(error));
exit (1);
}
s = -1;
for (res = res0; res; res = res->ai_next) {
s = socket (res->ai_family, res->ai_socktype, res->ai_protocol);
if (s < 0) {
fprintf (stderr, "socket: %s\n", strerror (errno));
exit (1);
}
if (connect(s, res->ai_addr, res->ai_addrlen) < 0) {
fprintf (stderr, "connect: %s\n", strerror (errno));
close(s);
exit (1);
}
break;
}
if (s != -1) {
get_page (s, host, "momoe/");
}
freeaddrinfo (res0);
return 0;
}


原文:http://www.lemoda.net/c/fetch-web-page/index.html
  评论这张
 
阅读(673)| 评论(0)
推荐 转载

历史上的今天

评论

<#--最新日志,群博日志--> <#--推荐日志--> <#--引用记录--> <#--博主推荐--> <#--随机阅读--> <#--首页推荐--> <#--历史上的今天--> <#--被推荐日志--> <#--上一篇,下一篇--> <#-- 热度 --> <#-- 网易新闻广告 --> <#--右边模块结构--> <#--评论模块结构--> <#--引用模块结构--> <#--博主发起的投票-->
 
 
 
 
 
 
 
 
 
 
 
 
 
 

页脚

网易公司版权所有 ©1997-2017