处理关于:空白/无IP地址

时间:2011-11-10 19:53:51

标签: windows sockets visual-c++

我正在运行代码来获取当前网页的网址和IP。这一切都很好,直到关于:空白显示,它停止我的代码冷。问题是关于:空白不返回IP地址,这就是问题所在。即使没有IP地址,如何保持运行。是的,它停止了。

ppaddr2 = (int**)pHostEnt2->h_addr_list;
sockAddr2.sin_addr.s_addr = **ppaddr2;
addr2 = inet_ntoa(sockAddr2.sin_addr);

相关代码......

BSTR bstr;
pBrowser->get_LocationURL(&bstr);
std::wstring wsURL;
wsURL = bstr;

size_t DSlashLoc = wsURL.find(L"://");
if (DSlashLoc != wsURL.npos)
    {
    wsURL.erase(wsURL.begin(), wsURL.begin() + DSlashLoc + 3);
    }
DSlashLoc = wsURL.find(L"www.");
if (DSlashLoc == 0)
    {
    wsURL.erase(wsURL.begin(), wsURL.begin() + 4);
    }
DSlashLoc = wsURL.find(L"/");
if (DSlashLoc != wsURL.npos)
    {
    wsURL.erase(DSlashLoc);
    }
   wprintf(L"\n   Current Website URL: %s", wsURL.c_str());

int Newlength = WideCharToMultiByte (CP_ACP, WC_COMPOSITECHECK, wsURL.c_str(), -1, NULL, 0,  NULL, NULL);
std::string NewLogURL(Newlength+1, 0); 
int Newresult = WideCharToMultiByte (CP_ACP, WC_COMPOSITECHECK, wsURL.c_str(), -1, &NewLogURL[0],Newlength+1,  NULL, NULL);

HOSTENT *pHostEnt2;
int  **ppaddr2;
SOCKADDR_IN sockAddr2;
pHostEnt2 = gethostbyname(NewLogURL.c_str());

ppaddr2 = (int**)pHostEnt2->h_addr_list;
sockAddr2.sin_addr.s_addr = **ppaddr2;
addr2 = inet_ntoa(sockAddr2.sin_addr);

printf("\n   Current Website IP:%s", addr2); 

2 个答案:

答案 0 :(得分:1)

gethostbyname如果找不到DNS条目,则返回NULL。只需检查并输出合适的消息

答案 1 :(得分:1)

除了rushman所说的,您还可以在处理之前验证URL是以“http:”还是“https:”开头。

另外,你不应该剥离“www”。一部分。 “://”和下一个“/”之间的所有内容都是主机名。按原样解决,DNS将处理它。事实上,如果你将其剥离,你可以得到不同的结果。例如,“yahoo.com”解析为“98.139.180.149”,但“www.yahoo.com”解析为“72.30.2.43”。浏览器不会剥离“www。”,所以你不应该。

试试这个:

BSTR bstr;
pBrowser->get_LocationURL(&bstr);
std::wstring wsURL(bstr, SysStringLen(bstr));
SysFreeString(bstr);

std::wstring wsHost;

size_t DSlashLoc = wsURL.find(L"://");
if (DSlashLoc != std::wstring::npos)
{
    DSlashLoc += 3;

    size_t DSlashLoc2 = wsURL.find(L"/", DSlashLoc);
    if (DSlashLoc2 != std::wstring::npos)
      wsHost = wsURL.substr(DSlashLoc, DSlashLoc2 - DSlashLoc);
    else
      wsHost = wsURL.substr(DSlashLoc);
}

if (wsHost.empty())
{
    printf("\n   Unknown Website Host");
}
else
{
    wprintf(L"\n   Website Host: %s", wsHost.c_str());

    std::string sHost;
    int Newlength = WideCharToMultiByte (CP_ACP, WC_COMPOSITECHECK, wsHost.c_str(), wsHost.length(), NULL, 0,  NULL, NULL);
    if (Newlength > 0)
    {
        sHost.resize(Newlength);
        WideCharToMultiByte (CP_ACP, WC_COMPOSITECHECK, wsHost.c_str(), wsHost.length(), &sHost[0], Newlength, NULL, NULL);
    }

    HOSTENT *pHostEnt2 = gethostbyname(sHost.c_str());
    if (!pHostEnt2)
    {
        printf("\n   Unknown Website IP");
    }
    else if (pHostEnt2->h_addrtype != AF_INET)
    {
        printf("\n   Website does not use IPv4 addresses");
    }
    else
    {
        for (char **addr = pHostEnt2->h_addr_list; *addr != NULL; ++addr)
        {
            printf("\n   Website IP: %s", inet_ntoa(* (struct in_addr *) *addr));
        }
    }
}