我們在進行google搜尋的時候,google為了安全起見,經常對於URL通過Google的安全過濾,以過濾網路木馬,如
下面我們來親自實踐,基於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");//GoogleUrlstd::cout << std::endl << "GoogleUrl" << std::endl;GURL gurl("http://user:123456@foo.tengattack.com: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;//得到queryif (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;}