http实现文件下载c语言,C语言winsock实现HTTP下载

该楼层疑似违规已被系统折叠 隐藏此楼查看此楼

#include

#include

#include

#include

#include //header

#pragma comment (lib, "ws2_32.lib") //lib

#define MAXSIZE 1024

void GET(char *url, char *host, char *file_name)

{

int length = 0;

char *url_end = (char *)(url + strlen(url));

char *host_begin, *host_end, *file;

/* 查找主机的开始位置 */

host_begin = strstr(url, "//") +2; //判断//是否是url的子串。如果是,则该函数返回//在url中首次出现的地址

host_end = strchr(host_begin, '/'); //查找字符串begin中首次出现:的位置

if (host_end == NULL)

{

host_end = url_end;

}

else

{ /* 得到文件名 */

file = strrchr(host_end, '/'); //返回指定字符最后一次出现位置的地址

if (file && (file + 1) != url_end)

strcpy(file_name, file + 1);

}

/* 得到主机信息 */

length = host_end - host_begin;

memcpy(host, host_begin, length);

host[length] = 0;

}

int main()

{

WSADATA wsa;

SOCKET sk;

sockaddr_in addr;

hostent * remoteHost;

FILE *fp;

char header[MAXSIZE],host[MAXSIZE],file_name[MAXSIZE];

char *http_req_hdr_tmpl = "GET %s HTTP/1.1\r\n"

"Accept: image/gif, image/jpeg, */*\r\nAccept-Language: zh-cn\r\n"

"Accept-Encoding: gzip, deflate\r\nHost: %s:%d\r\n"

"User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/31.0.1650.63 Safari/537.36\r\n"

"Connection: keep-alive\r\n\r\n";

char url[MAXSIZE];

int send_len,result;

scanf("%s",url); //输入URL

GET(url, host, file_name);

if( WSAStartup(MAKEWORD(1,1), &wsa) ){

printf("初始化失败!!!");

return 1;

}

if( (remoteHost = gethostbyname(host)) == NULL )

{

printf("通过主机名获取地址失败!!!");

return 1;

}

sk=socket(AF_INET,SOCK_STREAM,0);

if(sk==SOCKET_ERROR){

printf("设置socket错误!!!");

return 1;

}

//设置主机地址

addr.sin_family = AF_INET;

addr.sin_port = htons(80);

addr.sin_addr.s_addr = *((unsigned long *)*remoteHost->h_addr_list); //转换地址

if( connect(sk, (const sockaddr * )&addr, sizeof(addr)) )

{

printf("连接失败!!!");

closesocket(sk);

return 1;

}

//要发送的http头部

send_len = sprintf(header, http_req_hdr_tmpl, url, host, 80);

result = send(sk, header, send_len, 0);

if (result == SOCKET_ERROR) /* 发送失败 */

{

printf("发送失败!!! error = %d\n", WSAGetLastError());

return -1;

}

char path[MAXSIZE]={"C:/Users/Administrator/Desktop/"},buf[MAXSIZE],wbuf[MAXSIZE];

strcat(path,file_name);

fp=fopen(path, "w+");

bool done = FALSE;

int l,chars=0;

//打印并保存http响应的头部

while(!done)

{

l = recv(sk,buf,1,0);

if(l<0)

done=TRUE;

switch(*buf)

{

case '\r':

break;

case '\n':

if(chars==0)

done = TRUE;

chars=0;

break;

default:

chars++;

break;

}

printf("%c",*buf);

}

//接收正文部分

int sum = 0;

do{

l = recv(sk,buf,MAXSIZE-1,0);

if( l < 0 )

break;

sum += l;

*(buf + l) = 0;

fputs(buf,fp);

}while( l > 0 );

fflush(fp);

fclose(fp);

closesocket(sk);

WSACleanup();

return 0;

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值