C++:url reader 函数不适用于带有 \ 符号的 URL 链接。请帮助我

C++ : the url reader function doesn't work for URL links with \ sign. pls help me

如题中所写,这是returns一个URL.

的HTML源代码字符串的函数

它只适用于主页,例如"finance.yahoo.com/"。如果我从 Yahoo 中的书签之一尝试 URL,例如 "finance.yahoo.com/quote/BTC-USD/",它不起作用。

string getHtml() {

    HINTERNET hInternet = InternetOpenA("InetURL/1.0", INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0);

    HINTERNET hConnection = InternetConnectA(hInternet, " finance.yahoo.com/quote/BTC-USD/", 80, " ", " ", INTERNET_SERVICE_HTTP, 0, 0);

    HINTERNET hData = HttpOpenRequestA(hConnection, "GET", "/", NULL, NULL, NULL, INTERNET_FLAG_KEEP_CONNECTION, 0);

    char buf[2048];
    string lol;
    HttpSendRequestA(hData, NULL, 0, NULL, 0);

    DWORD bytesRead = 0;
    DWORD totalBytesRead = 0;
    int i = 0;
    string n, n2;

    while (InternetReadFile(hData, buf, 2000, &bytesRead) && bytesRead != 0)
    {
        buf[bytesRead] = 0; // insert the null terminator.

        // puts(buf);          // print it to the screen.
        lol = lol + buf;
        n = lol;
        totalBytesRead += bytesRead;
    }

    InternetCloseHandle(hData);
    InternetCloseHandle(hConnection);
    InternetCloseHandle(hInternet);

    return n;
}

这是错误的:

HINTERNET hConnection = InternetConnectA(hInternet, " finance.yahoo.com/quote/BTC-USD/", 80, " ", " ", INTERNET_SERVICE_HTTP, 0, 0);

HINTERNET hData = HttpOpenRequestA(hConnection, "GET", "/", NULL, NULL, NULL, INTERNET_FLAG_KEEP_CONNECTION, 0);

您不能像那样“连接”到 URL,只能连接到主机名。

您只需在 InternetConnectA() 中指定主机名,然后在 HttpOpenRequestA() 中指定与该主机名相关的所需文档。

此外,finance.yahoo.com 使用 HTTPS,而不是 HTTP,因此您需要连接到 HTTPS 端口并对请求使用安全语义。

试试这个:

HINTERNET hConnection = InternetConnectA(hInternet, "finance.yahoo.com", INTERNET_DEFAULT_HTTPS_PORT, " ", " ", INTERNET_SERVICE_HTTP, 0, 0);

HINTERNET hData = HttpOpenRequestA(hConnection, "GET", "/quote/BTC-USD/", NULL, NULL, NULL, INTERNET_FLAG_KEEP_CONNECTION | INTERNET_FLAG_SECURE, 0);