我们在进行google搜索的时候,google为了安全起见,经常对于URL通过谷歌的安全过滤,以过滤网络木马,如下图
下面我们来亲自实践,基于VC++来实现使用GoogleUrl方便安全地解析URL
#include "stdafx.h" #include "googleurl\src\gurl.h" void testURL(const char* lpszUrl) { char szDomain[256]; char szUrlPath[256]; szDomain[0] = 0; szUrlPath[0] = 0; const char *szStart = NULL; const char *szPathStart = NULL; if (strnicmp(lpszUrl, "http://", 7) == 0) { //http开头 szStart = lpszUrl + 7; } else if (strnicmp(lpszUrl, "https://", 8) == 0) { //https开头 szStart = lpszUrl + 8; } else { szStart = lpszUrl; } szPathStart = strstr(szStart, "/"); if (szPathStart) { strcpy(szUrlPath, szPathStart); } strncpy(szDomain, szStart, szPathStart - szStart); szDomain[szPathStart - szStart] = 0; printf("%s\nDomain: %s\nPath: %s\n", lpszUrl, szDomain, szUrlPath); } int _tmain(int argc, _TCHAR* argv[]) { std::cout << std::endl << "testURL" << std::endl; testURL("http://www.baidu.com/index.html"); //testURL("http://www.ABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZ.com/index.html?ABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZ"); //GoogleUrl std::cout << std::endl << "GoogleUrl" << std::endl; GURL gurl("http://user:[email protected]:8080/index.php?action=init"); std::cout<< "URL: " << gurl << std::endl << std::endl; //得到域名\主机地址 std::cout << "host() -> " << gurl.host() << std::endl; //得到端口号 if (gurl.has_port()) std::cout << "port() -> " << gurl.port() << std::endl; //得到query if (gurl.has_query()) std::cout << "query() -> " << gurl.query() << std::endl; //得到用户名密码 if (gurl.has_username()) std::cout << "username() -> " << gurl.username() << std::endl; if (gurl.has_password()) std::cout << "password() -> " << gurl.password() << std::endl; //得到文件名 std::cout << "ExtractFileName() -> " << gurl.ExtractFileName() << std::endl; //这个也是端口号,与port()的区别就是就算没有指定端口号默认会返回HTTP 80端口 std::cout << "EffectiveIntPort() -> " << gurl.EffectiveIntPort() << std::endl; //判断域名是否为IP地址 std::cout << "HostIsIPAddress() -> " << gurl.HostIsIPAddress() << std::endl; std::cin.get(); return 0; }