c++visual-c++wininet

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


As written in the title, this is a function which returns the HTML source code string of a URL.

It works only with the main page, such as "finance.yahoo.com/". If I try a URL from one of the bookmarks in Yahoo, such as "finance.yahoo.com/quote/BTC-USD/", it doesn't work.

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;
}

Solution

  • This is wrong:

    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);
    

    You can't "connect" to a URL like that, only to a hostname.

    You need to specify just the hostname in InternetConnectA(), and then specify the desired document relative to that hostname in HttpOpenRequestA().

    Also, finance.yahoo.com uses HTTPS, not HTTP, so you need to connect to the HTTPS port and use secure semantics on the request.

    Try this:

    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);