c语言http协议,HTTP协议的C语言编程实现实例(2)

c4468b3f4df77e96b0a416fa2a870fba.png

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(i24 == 0) fflush(fp);/*每1K时存盘一次*/

}

}

fclose(fp);

/* 结束通讯 */

close(sockfd);

exit(0);

}

//httpclient.c 结束///

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值