免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1006 | 回复: 1
打印 上一主题 下一主题

[学习分享] linux下的URL访问接口 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2015-06-17 10:48 |只看该作者 |倒序浏览
[C/C++]代码
  1. #include <stdio.h>
  2. #include <unistd.h>
  3. #include <sys/types.h>
  4. #include <sys/socket.h>
  5. #include <sys/stat.h>
  6. #include <fcntl.h>
  7. #include <string.h>
  8. #include <arpa/inet.h>
  9. #include <netdb.h>
  10. #include <netinet/in.h>
  11. #include <stdlib.h>
  12.   
  13.   
  14. #define BUFSIZE 0xF000
  15. void geturl(char* url)
  16. {
  17.         int cfd;
  18.         struct sockaddr_in cadd;
  19.         struct hostent *pURL = NULL;
  20.         char myurl[BUFSIZE];
  21.         char *pHost = 0;
  22.         char host[BUFSIZE],GET[BUFSIZE];
  23.         char request[BUFSIZE];
  24.         static char text[BUFSIZE];
  25.         int i,j;
  26.   
  27.   
  28.         //分离主机中的主机地址和相对路径
  29.         memset(myurl,0,BUFSIZE);
  30.         memset(host,0,BUFSIZE);
  31.         memset(GET,0,BUFSIZE);
  32.         strcpy(myurl,url);
  33.         for(pHost = myurl;*pHost != '/' && *pHost != '\0';++pHost);
  34.   
  35.   
  36.         //获取相对路径保存到GET中
  37.         if((int)(pHost-myurl) == strlen(myurl))
  38.         {
  39.                 strcpy(GET,"/");//即url中没有给出相对路径,需要自己手动的在url尾
  40. //部加上/
  41.         }
  42.         else
  43.         {
  44.                 strcpy(GET,pHost);//地址段pHost到strlen(myurl)保存的是相对路径
  45.         }
  46.         //将主机信息保存到host中
  47.         //此处将它置零,即它所指向的内容里面已经分离出了相对路径,剩下的为host信
  48. //息(从myurl到pHost地址段存放的是HOST)
  49.         *pHost = '\0';
  50.         strcpy(host,myurl);
  51.         //设置socket参数
  52.         if(-1 == (cfd = socket(AF_INET,SOCK_STREAM,0)))
  53.         {
  54.                 printf("create socket failed of client!\n");
  55.                 exit(-1);
  56.         }
  57.   
  58.         pURL = gethostbyname(host);//将上面获得的主机信息通过域名解析函数获得域>名信息
  59.   
  60.         //设置IP地址结构
  61.         bzero(&cadd,sizeof(struct sockaddr_in));
  62.         cadd.sin_family = AF_INET;
  63.         cadd.sin_addr.s_addr = *((unsigned long*)pURL->h_addr_list[0]);
  64.         cadd.sin_port = htons(80);
  65.         //向WEB服务器发送URL信息
  66.         memset(request,0,BUFSIZE);
  67.         strcat(request,"GET ");
  68.         strcat(request,GET);
  69.         strcat(request," HTTP/1.1\r\n");//至此为http请求行的信息
  70.         strcat(request,"HOST: ");
  71.         strcat(request,host);
  72.         strcat(request,"\r\n");
  73.         strcat(request,"Cache-Control: no-cache\r\n\r\n");
  74.         //连接服务器
  75. int cc;
  76.         if(-1 == (cc = connect(cfd,(struct sockaddr*)&cadd,(socklen_t)sizeof(cadd))))
  77.         {
  78.                 printf("connect failed of client!\n");
  79.                 exit(1);
  80.         }
  81.         printf("connect success!\n");
  82.   
  83.         //向服务器发送url请求的request
  84.         int cs;
  85.         if(-1 == (cs = send(cfd,request,strlen(request),0)))
  86.         {
  87.                 printf("向服务器发送请求的request失败!\n");
  88.                 exit(1);
  89.         }
  90.         printf("发送成功,发送的字节数:%d\n",cs);
  91.   
  92.         //客户端接收服务器的返回信息
  93.         memset(text,0,BUFSIZE);
  94.         int cr;
  95.         if(-1 == (cr = recv(cfd,text,BUFSIZE,0)))
  96.         {
  97.                 printf("recieve failed!\n");
  98.                 exit(1);
  99.         }
  100.         else
  101.         {
  102.                 printf("receive succecc!\n");
  103.         }
  104.         close(cfd);
  105. }
  106.   
  107. int main(int argc,char* argv[])
  108. {
  109.         if(argc<2)
  110.         {
  111.                 printf("用法:%c url网页网址\n",argv[0]);
  112.                 exit(1);
  113.         }
  114.         geturl(argv[1]);
  115.         return 0;
  116. }
复制代码

论坛徽章:
0
2 [报告]
发表于 2015-06-17 18:18 |只看该作者
很不错呀!!!!
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP