歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> Linux下C語言url請求

Linux下C語言url請求

日期:2017/3/1 9:44:42   编辑:Linux編程

任何代碼只有你寫過,才能深刻理解,有的時候看文檔看的很茫然的時候,盡量找點別人的源碼,在別人源碼上更改,在調試,可能會達到事半功倍的效果。

近來項目需要Linux中訪問一個url接口傳參數及獲取返回值,看了很多方法,知道用socket,但是沒接觸過,感覺很茫然,就在網上找實例,看看了,有了一點感覺,又在別人的實例上改動之後竟然可以用了,當時就卡在了http的格式上,下面寫個demo,只要在我的代碼上吧url換了,肯定可以成功。注意url不要http://

Linux下關於時間概念的C語言編程 http://www.linuxidc.com/Linux/2014-04/99747.htm

C++ 隱式類類型轉化 Implicit Class-Type Conversions http://www.linuxidc.com/Linux/2013-01/78071.htm

C語言變長數組之剖析 http://www.linuxidc.com/Linux/2013-07/86997.htm

C語言需要注意的問題 http://www.linuxidc.com/Linux/2013-05/84301.htm

C語言位域的使用及其注意點 http://www.linuxidc.com/Linux/2013-07/87027.htm

C語言中簡單的for循環和浮點型變量 http://www.linuxidc.com/Linux/2013-08/88514.htm

《C語言從入門到精通》.(王娣,韓旭 ).[PDF] + DVD視頻光盤文件 http://www.linuxidc.com/Linux/2013-10/91775.htm

#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;

//分離主機中的主機地址和相對路徑
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);
int cr;
if(-1 == (cr = recv(cfd,text,BUFSIZE,0)))
{
printf("recieve failed!\n");
exit(1);
}
else
{
printf("receive succecc!\n");
}
close(cfd);
}

int main(int argc,char* argv[])
{
if(argc<2)
{
printf("用法:%c url網頁網址\n",argv[0]);
exit(1);
}
geturl(argv[1]);
return 0;
}

Copyright © Linux教程網 All Rights Reserved