"); //-->
用C 实现的一个URL解析的小程序。留个记录在这里。
#include "string.h"
#include "stdio.h"
char URL[]="http://bitgarden.test.wimang.com:80/hms/hardware/soft/updateSoft.do?softwareVersion=1.1&hardwareVersion=1.0";
//char URL[]="bitgarden.test.wimang.com:80/hms/hardware/soft/updateSoft.do?softwareVersion=1.1&hardwareVersion=1.0";
//char URL[]="bitgarden.test.wimang.com/hms/hardware/soft/updateSoft.do?softwareVersion=1.1&hardwareVersion=1.0";
char ADDR[30];
int PORT=0;
char GETContext[100]="0";
void getAddr(char *url,int len)
{
char *storeAddr;
int k=0;
int m=0;
static const char headline[]="http://";
// 定位 开头
if(strstr(url,headline) !=NULL)
{
storeAddr=&(url[sizeof(headline)]);
}
else
{
storeAddr=url;
}
if(strchr(storeAddr,':')!=NULL)
{
for(k=0;k<len;k++)
{
if(storeAddr[k]==':')
{
break;
}
}
strncpy(ADDR,storeAddr,k);
printf("HOSTis%s\r\n",ADDR);
storeAddr=&(storeAddr[k]);//:80/hms
PORT=atoi(storeAddr+1);
/*for(m=0;m<len;m++)
{
if(storeAddr[m]=='/')
{
break;
}
}*/
// storeAddr=&(storeAddr[m]);// /hms
storeAddr=strchr(storeAddr,'/');
strcpy(GETContext,storeAddr);
printf("port is %d\r\n",PORT) ;
printf("GET %s\r\n",GETContext);
}
/* else if(strchr(storeAddr,'/')!=NULL)
{
for(k=0;k<len;k++)
{
if(storeAddr[k]=='/')
{
break;
}
}
strncpy(ADDR,storeAddr,k);
printf("HOSTis%s\r\n",ADDR);
PORT=80;
}*/
}
int main()
{
getAddr(URL,strlen(URL)) ;
}
*博客内容为网友个人发布,仅代表博主个人观点,如有侵权请联系工作人员删除。