解析URL的数据

网络协议

Posted by YiMiTuMi on March 21, 2019

解析URL的数据

上次说过URL概念,但具体在程序中将其中数据解析出来我是用windows函数InternetCrackUrl来实现的,尾后的W和A是函数的编码方式。

程序如下:

#include <windows.h>
#include <WinInet.h>
#include <iostream>
#include <string>
#pragma comment(lib, "WinInet.lib") 

using namespace std;

void SplitURL(wstring lpUrl)
{
  do 
  {
    URL_COMPONENTS urlComp = { 0 };

    WCHAR Scheme[MAX_PATH] = { 0 };
    WCHAR HostName[MAX_PATH] = { 0 };
    WCHAR UserName[MAX_PATH] = { 0 };
    WCHAR Password[MAX_PATH] = { 0 };
    WCHAR UrlPath[MAX_PATH] = { 0 };
    WCHAR ExtraInfo[MAX_PATH] = { 0 };

    urlComp.dwSchemeLength = MAX_PATH;
    urlComp.dwHostNameLength = MAX_PATH;
    urlComp.dwUserNameLength = MAX_PATH;
    urlComp.dwPasswordLength = MAX_PATH;
    urlComp.dwUrlPathLength = MAX_PATH;
    urlComp.dwExtraInfoLength = MAX_PATH;
    
    urlComp.dwStructSize = sizeof(urlComp);
    urlComp.lpszScheme = Scheme;
    urlComp.lpszHostName = HostName;
    urlComp.lpszUserName = UserName;
    urlComp.lpszPassword = Password;
    urlComp.lpszUrlPath = UrlPath;
    urlComp.lpszExtraInfo = ExtraInfo;
    //调用函数
    if (InternetCrackUrlW(lpUrl.c_str(), lpUrl.size(), NULL, &urlComp) == FALSE)
    {
        break;
    }
        wcout << "Scheme: " << urlComp.lpszScheme << endl;
        wcout << "HostName: " << urlComp.lpszHostName << endl; //主机名
        wcout << "UserName: " << urlComp.lpszUserName << endl; //用户名
        wcout << "Password: " << urlComp.lpszPassword << endl; //密码
        wcout << "UrlPath: " << urlComp.lpszUrlPath << endl;   //网址路径
        wcout << "ExtraInfo: " << urlComp.lpszExtraInfo << endl; 
        wcout << "nPort: " << urlComp.nPort << endl;           //端口号
        
  } while (FALSE);
}
int main()
{
    wstring zurl = L"http://www.demongan.com/upload.asp?file=520.zip";
    wcout << zurl.c_str() <<endl;
    SplitURL(zurl);
    system("PAUSE");
    return 0;
};

最后输出结果:

http://www.demongan.com/upload.asp?file=520.zi
Scheme: http
HostName: www.demongan.com
UserName:
Password:
UrlPath: /upload.asp
ExtraInfo: ?file=520.zip
nPort: 80
请按任意键继续. . .

月光花 – 易碎的美好