I am attempting to load HTML code using C++ and wininet.h instrumentation in Visual Studio 2010 Express. To create the URL String, I write the following:
HINTERNET hConnect =::InternetConnect(
hInternet,
_T("stackoverflow.com"),
INTERNET_DEFAULT_HTTP_PORT,
NULL,
NULL,
IINTERNET_SERVICE_HTTP,
0,
1u
);
The program works well when the URL is just a domain with no path or protocol, but when I change the URL from stackoverflow.com to http://stackoverflow.com/questions, suddenly my code doesn’t work any longer.
HttpSendRequest(hRequest, NULL, 0, NULL, 0) simply returns false.
I have tried a number of different variations on my parameters and have scoured Google, but I am just plain stuck. Can anyone provide some insight?
The documentation for
InternetConnecttells me that it is used to establish an FTP or HTTP connection to a server – not to download a URL.As such, the
lpszServerNameparameter is documented as:So you must use a server’s host name or IP address and cannot use a URL, hence why it doesn’t work when you do.
The path part of your URL (
/questions) should be used as thelpszObjectNameparameter of aHttpOpenRequestcall (docs) that you make using thehConnectreturned by yourInternetConnectcall.