linux c用url請求下載檔案
阿新 • • 發佈:2019-01-03
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <string.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <netinet/in.h>
#include <stdlib.h>
#define BUFSIZE 0xF000
void geturl(char* url)
{
int cfd;
struct sockaddr_in cadd;
struct hostent *pURL = NULL;
char myurl[BUFSIZE];
char *pHost = 0;
char host[BUFSIZE],GET[BUFSIZE];
char request[BUFSIZE];
static char text[BUFSIZE];
int i,j;
int fd = open("./hah.mp3",O_WRONLY|O_CREAT,777);
if(fd == -1)
{
printf("OPen error\n");
exit(1);
}
//分離主機中的主機地址和相對路徑
memset(myurl,0,BUFSIZE);
memset(host,0,BUFSIZE);
memset(GET,0,BUFSIZE);
strcpy(myurl,url);
for(pHost = myurl;*pHost != '/' && *pHost != '\0';++pHost);
//獲取相對路徑儲存到GET中
if((int)(pHost-myurl) == strlen(myurl))
{
strcpy(GET,"/");//即url中沒有給出相對路徑,需要自己手動的在url尾
//部加上/
}
else
{
strcpy(GET,pHost);//地址段pHost到strlen(myurl)儲存的是相對路徑
}
//將主機資訊儲存到host中
//此處將它置零,即它所指向的內容裡面已經分離出了相對路徑,剩下的為host信
//息(從myurl到pHost地址段存放的是HOST)
*pHost = '\0';
strcpy(host,myurl);
//設定socket引數
if(-1 == (cfd = socket(AF_INET,SOCK_STREAM,0)))
{
printf("create socket failed of client!\n");
exit(-1);
}
pURL = gethostbyname(host);//將上面獲得的主機資訊通過域名解析函式獲得域>名資訊
//設定IP地址結構
bzero(&cadd,sizeof(struct sockaddr_in));
cadd.sin_family = AF_INET;
cadd.sin_addr.s_addr = *((unsigned long*)pURL->h_addr_list[0]);
cadd.sin_port = htons(80);
//向WEB伺服器傳送URL資訊
memset(request,0,BUFSIZE);
strcat(request,"GET ");
strcat(request,GET);
strcat(request," HTTP/1.1\r\n");//至此為http請求行的資訊
strcat(request,"HOST: ");
strcat(request,host);
strcat(request,"\r\n");
strcat(request,"Cache-Control: no-cache\r\n\r\n");
//連線伺服器
int cc;
if(-1 == (cc = connect(cfd,(struct sockaddr*)&cadd,(socklen_t)sizeof(cadd))))
{
printf("connect failed of client!\n");
exit(1);
}
printf("connect success!\n");
//向伺服器傳送url請求的request
int cs;
if(-1 == (cs = send(cfd,request,strlen(request),0)))
{
printf("向伺服器傳送請求的request失敗!\n");
exit(1);
}
printf("傳送成功,傳送的位元組數:%d\n",cs);
//客戶端接收伺服器的返回資訊
memset(text,0,BUFSIZE);
while(1){
int cr;
cr = recv(cfd,text,BUFSIZE,0);
if(cr <= 0) break;
write(fd,text,cr);
printf("receive succecc!\n");
printf("Message:%s\n",text);
}
close(fd);
close(cfd);
}
int main(int argc,char* argv[])
{
if(argc<2)
{
printf("用法:%c url網頁網址\n",argv[0]);
exit(1);
}
geturl(argv[1]);
return 0;
}
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <string.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <netinet/in.h>
#include <stdlib.h>
#define BUFSIZE 0xF000
void geturl(char* url)
{
int cfd;
struct sockaddr_in cadd;
struct hostent *pURL = NULL;
char myurl[BUFSIZE];
char *pHost = 0;
char host[BUFSIZE],GET[BUFSIZE];
char request[BUFSIZE];
static char text[BUFSIZE];
int i,j;
int fd = open("./hah.mp3",O_WRONLY|O_CREAT,777);
if(fd == -1)
{
printf("OPen error\n");
exit(1);
}
//分離主機中的主機地址和相對路徑
memset(myurl,0,BUFSIZE);
memset(host,0,BUFSIZE);
memset(GET,0,BUFSIZE);
strcpy(myurl,url);
for(pHost = myurl;*pHost != '/' && *pHost != '\0';++pHost);
//獲取相對路徑儲存到GET中
if((int)(pHost-myurl) == strlen(myurl))
{
strcpy(GET,"/");//即url中沒有給出相對路徑,需要自己手動的在url尾
//部加上/
}
else
{
strcpy(GET,pHost);//地址段pHost到strlen(myurl)儲存的是相對路徑
}
//將主機資訊儲存到host中
//此處將它置零,即它所指向的內容裡面已經分離出了相對路徑,剩下的為host信
//息(從myurl到pHost地址段存放的是HOST)
*pHost = '\0';
strcpy(host,myurl);
//設定socket引數
if(-1 == (cfd = socket(AF_INET,SOCK_STREAM,0)))
{
printf("create socket failed of client!\n");
exit(-1);
}
pURL = gethostbyname(host);//將上面獲得的主機資訊通過域名解析函式獲得域>名資訊
//設定IP地址結構
bzero(&cadd,sizeof(struct sockaddr_in));
cadd.sin_family = AF_INET;
cadd.sin_addr.s_addr = *((unsigned long*)pURL->h_addr_list[0]);
cadd.sin_port = htons(80);
//向WEB伺服器傳送URL資訊
memset(request,0,BUFSIZE);
strcat(request,"GET ");
strcat(request,GET);
strcat(request," HTTP/1.1\r\n");//至此為http請求行的資訊
strcat(request,"HOST: ");
strcat(request,host);
strcat(request,"\r\n");
strcat(request,"Cache-Control: no-cache\r\n\r\n");
//連線伺服器
int cc;
if(-1 == (cc = connect(cfd,(struct sockaddr*)&cadd,(socklen_t)sizeof(cadd))))
{
printf("connect failed of client!\n");
exit(1);
}
printf("connect success!\n");
//向伺服器傳送url請求的request
int cs;
if(-1 == (cs = send(cfd,request,strlen(request),0)))
{
printf("向伺服器傳送請求的request失敗!\n");
exit(1);
}
printf("傳送成功,傳送的位元組數:%d\n",cs);
//客戶端接收伺服器的返回資訊
memset(text,0,BUFSIZE);
while(1){
int cr;
cr = recv(cfd,text,BUFSIZE,0);
if(cr <= 0) break;
write(fd,text,cr);
printf("receive succecc!\n");
printf("Message:%s\n",text);
}
close(fd);
close(cfd);
}
int main(int argc,char* argv[])
{
if(argc<2)
{
printf("用法:%c url網頁網址\n",argv[0]);
exit(1);
}
geturl(argv[1]);
return 0;
}