Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / Managed C++ / August 2004

Tip: Looking for answers? Try searching our database.

How can I access an URL through the code

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
ssunderk - 26 Aug 2004 02:24 GMT
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.
William DePalo [MVP VC++] - 26 Aug 2004 03:01 GMT
> 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
Carl Daniel [VC++ MVP] - 26 Aug 2004 04:46 GMT
> 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

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.