大家都很熟悉HTTP协议的应用,因为每天都在网络上浏览着不少东西,也都知道是HTTP协议是相当简单的。每次用thunder之类的下载软件下载网页,当用到那个“用thunder下载全部链接”时总觉得很神奇。
后来想想,其实要实现这些下载功能也并不难,只要按照HTTP协议发送request,然后对接收到的数据进行分析,如果页面上还有href之类的链接指向标志就可以进行深一层的下载了。,要全面透彻地搞懂它就参考RFC2616文档吧。
源代码如下:
/******* http客户端程序 ************/
#include <>
#include <>
#include <>
#include <sys/>
#include <sys/>
#include <>
#include <>
#include </>
#include <>
#include <>
#include <arpa/>
#include <>
////////////////////////////// 开始///////////////////////////////////////////
/********************************************
功能:搜索字符串右边起的第一个匹配字符
********************************************/
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) {*s=tolower(*s);s++;}
}
/**************************************************************
功能:从字符串src中分析出网站地址和端口,并得到用户要下载的文件
***************************************************************/
void GetHost(char * src, char * web, char * file, int * port) {
char * pA;
char * pB;
memset(web, 0, sizeof(web));
http解析 来自淘豆网www.taodocs.com转载请标明出处.