http下载网页
//http.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <errno.h>
#include <unistd.h>
#include <netinet/in.h>
#include <limits.h>
#include <netdb.h>
#include <arpa/inet.h>
#include <ctype.h>
/********************************************
功能:搜索字符串右边起的第一个匹配字符
********************************************/
char * Rstrchr(char * s, char x) {
int i = strlen(s);
if(!(*s)) return 0;
while(s[i-1]) if(strchr(s + (i - 1), x)) return (s + (i - 1)); else i--;
return 0;
}
/********************************************
功能:把字符串转换为全小写
********************************************/
void ToLowerCase(char * s) {
while(*s) *s=tolower(*s++);
}
/**************************************************************
功能:从字符串src中分析出网站地址和端口,并得到用户要下载的文件
***************************************************************/
void GetHost(char * src, char * web, char * file, int * port) {
char * pA;
char * pB;
memset(web, 0, sizeof(web));
memset(file, 0, sizeof(file));
*port = 0;
if(!(*src)) return;
pA = src;
if(!strncmp(pA, "http://", strlen("http://"))) pA = src+strlen("http://");
else if(!strncmp(pA, "https://", strlen("https://"))) pA = src+strlen("https://");
pB = strchr(pA, '/');
if(pB) {
memcpy(web, pA, strlen(pA) - strlen(pB));
if(pB+1) {
memcpy(file, pB + 1, strlen(pB) - 1);
file[strlen(pB) - 1] = 0;
}
}
else memcpy(web, pA, strlen(pA));
if(pB) web[strlen(pA) - strlen(pB)] = 0;
else web[strlen(pA)] = 0;
pA = strchr(web, ':');
if(pA) *port = atoi(pA + 1);
else *port = 80;
}
/*********************************************************************
*filename: http.c
*purpose: HTTP协议客户端程序,可以用来下载网页 下载http服务器的文件
*********************************************************************/
int main(int argc, char *argv[])
{
int sockfd;
char buffer[1024];
struct sockaddr_in server_addr;
struct hostent *host;
int portnumber,nbytes;
char host_addr[256];
char host_file[1024];
char local_file[256];
FILE * fp;
char request[1024];
int send, totalsend;
int i;
char * pt;
if(argc!=2)
{
fprintf(stderr,"Usage:%s web-address\a\n",argv[0]);
exit(1);
}
printf("parameter.1 is: %s\n", argv[1]);
// ToLowerCase(argv[1]);/*将参数转换为全小写*/
printf("lowercase parameter.1 is: %s\n", argv[1]);
GetHost(argv[1], host_addr, host_file, &portnumber);/*分析网址、端口、文件名等*/
printf("webhost:%s\n", host_addr);
printf("hostfile:%s\n", host_file);
printf("portnumber:%d\n\n", portnumber);
if((host=gethostbyname(host_addr))==NULL)/*取得主机IP地址*/
{
fprintf(stderr,"Gethostname error, %s\n", strerror(errno));
exit(1);
}
/* 客户程序开始建立 sockfd描述符 */
if((sockfd=socket(AF_INET,SOCK_STREAM,0))==-1)/*建立SOCKET连接*/
{
fprintf(stderr,"Socket Error:%s\a\n",strerror(errno));
exit(1);
}
/* 客户程序填充服务端的资料 */
bzero(&server_addr,sizeof(server_addr));
server_addr.sin_family=AF_INET;
server_addr.sin_port=htons(portnumber);
server_addr.sin_addr=*((struct in_addr *)host->h_addr);
/* 客户程序发起连接请求 */
if(connect(sockfd,(struct sockaddr *)(&server_addr),sizeof(struct sockaddr))==-1)/*连接网站*/
{
fprintf(stderr,"Connect Error:%s\a\n",strerror(errno));
exit(1);
}
sprintf(request, "GET /%s HTTP/1.1\r\nAccept: */*\r\nAccept-Language: zh-cn\r\n\
User-Agent: Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)\r\n\
Host: %s:%d\r\nConnection: Close\r\n\r\n", host_file, host_addr, portnumber);
printf("%s", request);/*准备request,将要发送给主机*/
/*取得真实的文件名*/
if(host_file && *host_file) pt = Rstrchr(host_file, '/');
else pt = 0;
memset(local_file, 0, sizeof(local_file));
if(pt && *pt) {
if((pt + 1) && *(pt+1)) strcpy(local_file, pt + 1);
else memcpy(local_file, host_file, strlen(host_file) - 1);
}
else if(host_file && *host_file) strcpy(local_file, host_file);
else strcpy(local_file, "index.html");
printf("local filename to write:%s\n\n", local_file);
/*发送http请求request*/
send = 0;totalsend = 0;
nbytes=strlen(request);
while(totalsend < nbytes) {
send = write(sockfd, request + totalsend, nbytes - totalsend);
if(send==-1) {printf("send error!%s\n", strerror(errno));exit(0);}
totalsend+=send;
printf("%d bytes send OK!\n", totalsend);
}
fp = fopen(local_file, "a");
if(!fp) {
printf("create file error! %s\n", strerror(errno));
return 0;
}
printf("\nThe following is the response header:\n");
i=0;
/* 连接成功了,接收http响应,response */
while((nbytes=read(sockfd,buffer,1))==1)
{
if(i < 4) {
if(buffer[0] == '\r' || buffer[0] == '\n') i++;
else i = 0;
printf("%c", buffer[0]);/*把http头信息打印在屏幕上*/
}
else {
fwrite(buffer, 1, 1, fp);/*将http主体信息写入文件*/
i++;
if(i%1024 == 0) fflush(fp);/*每1K时存盘一次*/
}
}
fclose(fp);
/* 结束通讯 */
close(sockfd);
exit(0);
}
//******************************************//
linux下编译
命令:
gcc http.c -o http
ls
./http http://baidu.com/test.html
显示:
webhost:www.baidu.com
hostfile:
portnumber:80
GET / HTTP/1.1
Accept: */*
Accept-Language: zh-cn
User-Agent: Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)
Host: www.baidu.com:80
Connection: Close
local filename to write:index.html
163 bytes send OK!
The following is the response header:
HTTP/1.1 200 OK
Date: Thu, 16 Nov 2017 03:23:19 GMT
Content-Type: text/html; charset=utf-8
Transfer-Encoding: chunked
Connection: Close
Vary: Accept-Encoding
Set-Cookie: BAIDUID=A8834922B6D30BB2F14E43B4EF6494FF:FG=1; expires=Thu, 31-Dec-37 23:55:55 GMT; max-age=2147483647; path=/; domain=.baidu.com
Set-Cookie: BIDUPSID=A8834922B6D30BB2F14E43B4EF6494FF; expires=Thu, 31-Dec-37 23:55:55 GMT; max-age=2147483647; path=/; domain=.baidu.com
Set-Cookie: PSTM=1510802599; expires=Thu, 31-Dec-37 23:55:55 GMT; max-age=2147483647; path=/; domain=.baidu.com
Set-Cookie: BDSVRTM=0; path=/
Set-Cookie: BD_HOME=0; path=/
Set-Cookie: H_PS_PSSID=1430_24868_13290_21110_24879; path=/; domain=.baidu.com
P3P: CP=" OTI DSP COR IVA OUR IND COM "
Cache-Control: private
Cxy_all: baidu+9252c7ff064baeb146003978af4dad38
Expires: Thu, 16 Nov 2017 03:22:59 GMT
X-Powered-By: HPHP
Server: BWS/1.1
X-UA-Compatible: IE=Edge,chrome=1
BDPAGETYPE: 1
BDQID: 0xd0d6dd0d0000eec0
BDUSERID: 0
不指定文件名字的话,默认就是下载网站默认的首页了
http下载网页的更多相关文章
- Python下载网页的几种方法
get和post方式总结 get方式:以URL字串本身传递数据参数,在服务器端可以从'QUERY_STRING'这个变量中直接读取,效率较高,但缺乏安全性,也无法来处理复杂的数据(只能是字符串,比如在 ...
- JavaWeb学习之转发和重定向、会话技术:cookie、session、验证码实例、URLConnection使用(下载网页)(4)
1.转发和重定向 HttpServletResponse response 转发: RequestDispatcher dispatcher = request.getRequestDispatche ...
- windows phone 7,sliverlight 下载网页的解析,关于wp7 gb2312编码
原文:windows phone 7,sliverlight 下载网页的解析,关于wp7 gb2312编码 关于silverlight和wp7(windows phone 7)是默认不支持gb2312 ...
- Java的URL来下载网页源码
import java.io.BufferedInputStream; import java.io.BufferedReader; import java.io.IOException; impor ...
- PHP下载网页
<?php /* author:whq 作用:获取网页的内容 */ include "../Snoopy/Snoopy.class.php";class Cute ...
- Java如何读取和下载网页?
在Java编程中,如何读取和下载网页? 以下示例显示如何使用net.URL类的URL()构造函数来读取和下载网页. package com.yiibai; import java.io.Buffere ...
- C# 网络编程之webBrowser获取网页url和下载网页中图片
该文章主要是通过C#网络编程的webBrowser获取网页中的url并简单的尝试瞎子啊网页中的图片,主要是为以后网络开发的基础学习.其中主要的通过应用程序结合网页知识.正则表达式实现浏览.获取url. ...
- 用firefox的插件下载网页中的视频
对于网页中的一些视频,直接下载不了,可以用专用下载软件下载,也可以用firefox的NetVideohunter Video Downloader插件下载网页中的视频,方便快捷. 工具/原料 fi ...
- Lynx以纯文本的形式下载网页
Lynx是一款基于命令行的web浏览器 [root@test88 ~]# yum install lynx -y [root@test88 ~]# lynx www.baidu.com 以纯文本的形式 ...
随机推荐
- 前端基础之初识 HTML
HTML HTML(Hypertext Markup Language)即超文本标记语言,是WWW的描述语言.设计HTML语言的目的是为了能把存放在一台电脑中的文本或图形与另一台电脑中的文本或图形方便 ...
- C#.net干货,最全公共帮助类
比较全面的c#帮助类,日常工作收集,包括前面几家公司用到的,各式各样的几乎都能找到,所有功能性代码都是独立的类,类与类之间没有联系,可以单独引用至项目,分享出来,方便大家,几乎都有注释,喜欢的请点赞, ...
- C# JAVA成员访问修饰符比较
在面向对象的访问修饰符中常用的有public ,private ,protected C# 访问修饰符: private < protected internal < internal/p ...
- 【转载】quickLayout.css-快速构建结构兼容的web页面
文章转载自 张鑫旭-鑫空间-鑫生活 http://www.zhangxinxu.com/wordpress/ 原文链接:http://www.zhangxinxu.com/wordpress/?p=4 ...
- python 中的enumerate()函数的用法
enumerate函数说明: 函数语法:enumerate(可遍历的对象,索引号开始的值).enumerate(sequence, [start=0]) 功能:将可循环序列sequence以start ...
- WPF DataGridHyperlinkColumn
为了点击链接,我们使用 <DataGrid x:Name="data" LoadingRow="load" ItemsSource="{Bind ...
- 基于FFMPEG的跨平台播放器实现
基于FFMPEG的跨平台播放器实现 一.背景介绍 FFmpeg是一款超级强大的开源多媒体编解码框架,提供了录制.转换以及流化音视频的完整解决方案,包含了libavcodec.libavformat等多 ...
- 什么是UUID?
1.定义 UUID含义是通用唯一识别码 (Universally Unique Identifier),这 是一个软件建构的标准,也是被开源软件基金会 (Open Software Foundatio ...
- hibernate5使用注解遇到的问题
问题描述 出现MappingException:Unknown entity,看到这个我以为在cfg配置文件中没有配置,实际上我是配置了的,那么问题出在那里呢,既然找不到实体,那么会不会是注解类出现了 ...
- 安装debian 9.1后,中文环境下将home目录下文件夹改为对应的英文
安装了debian 9.1后,中文环境下home目录下文件夹显示的是中文,相当不方便cd命令,改为对应的英文吧,需要用到的软件xdg-user-dirs-gtk #安装需要的软件 sudo apt i ...