In my program, I should be able to access URL using the code in VC++/VB.
I would appreciate if anybody can provide a sample code.
> In my program, I should be able to access URL using the code in VC++/VB.
> I would appreciate if anybody can provide a sample code.
This is a bare-bones pseudo code sketch using functions in the WinInet API:
InternetConnect(); // Open an HTTP session
HttpOpenRequest(); // Compose an HTTP "GET" transaction
HttpSendRequest(); // Send the "GET" to the web server
HttpQueryInfo(); // Determine the length of the response
InternetReadFile(); // Read the response
InternetCloseHandle(); // Close the HTTP request
InternetCloaseHandle(); // Close the HTTP session
The WinHTTP API is a newer alternative but supported on fewer platforms,
IIRC.
Regards,
Will
> In my program, I should be able to access URL using the code in
> VC++/VB. I would appreciate if anybody can provide a sample code.
Here's a minimal C++ program to fetch the document from an URL and save it
to a local file.
<code>
#include <windows.h>
#include <tchar.h>
#include <urlmon.h>
#include <stdio.h>
int _tmain(int argc, TCHAR* argv[])
{
if (3 != argc)
{
_tprintf(_T("usage: %0 URL out-file-name"),argv[0]);
return 1;
}
::CoInitialize(0);
TCHAR szFullName[MAX_PATH];
::GetFullPathName(argv[2],MAX_PATH,szFullName,0);
HRESULT hr = ::URLDownloadToFile(
0,
argv[1],
szFullName,
0,
0
);
::CoUninitialize();
if (FAILED(hr))
{
_tprintf(_T("Failed, hr=0x%08x\n"),hr);
return 2;
}
return 0;
}
<code>
You can also very easily fetch from URLs by using the URL Moniker. IIRC,
you can simply call CoGetObject, passing an URL as the "Display Name" and
requesting IStream as the interface. From that IStream you can read the
document into memory, copy it to disk, or whatever.
-cd