Hello everyone,

I 've been looking for a solution for this problem for some time now but I don't seem to figure it out.

I want to download the source of a web page (the HTML source). I found several API function that have to do something with internet and downloading for example InternetOpen, InternetOpenUrl .

This didn't work. I tried almost everything but it wouldn't compile.

I found this code on MSDN:

int main(int argc, char* argv[])
{
	HINTERNET hNet = ::InternetOpen("MSDN SurfBear",
                                PRE_CONFIG_INTERNET_ACCESS,
                                NULL,
                                INTERNET_INVALID_PORT_NUMBER,
                                0) ;

HINTERNET hUrlFile = ::InternetOpenUrl(hNet,
                                "http://www.microsoft.com",
                                NULL,
                                0,
                                INTERNET_FLAG_RELOAD,
                                0) ;

char buffer[10*1024] ;
DWORD dwBytesRead = 0;
BOOL bRead = ::InternetReadFile(hUrlFile,
                                buffer,
                                sizeof(buffer),
                                &dwBytesRead);

::InternetCloseHandle(hUrlFile) ;

::InternetCloseHandle(hNet) ;
	return 0;
}

It keeps giving me the following errors:

--------------------Configuration: internetopen - Win32 Debug--------------------
Linking...
internetopen.obj : error LNK2001: unresolved external symbol __imp__InternetCloseHandle@4
internetopen.obj : error LNK2001: unresolved external symbol __imp__InternetReadFile@16
internetopen.obj : error LNK2001: unresolved external symbol __imp__InternetOpenUrlA@24
internetopen.obj : error LNK2001: unresolved external symbol __imp__InternetOpenA@20
Debug/internetopen.exe : fatal error LNK1120: 4 unresolved externals
Error executing link.exe.

internetopen.exe - 5 error(s), 0 warning(s)

One project I downloaded does work. A bit. It downloads the first several lines of the source and then just outputs fffffffffffffffffffffffffffffffff (it the buffer gets filler with ff-s after the first lines of source).

Also this is an MFC project, and I prefer not using MFC.

I'll post the MFC code below:

void CMFCinternetDlg::OnButton1() 
{
	// TODO: Add your control notification handler code here
	CInternetSession* pInternetSession;
	pInternetSession = new CInternetSession();

	if (!pInternetSession)
	{
		MessageBox("Could not establish HTTP session, please check your connection","error");
		return;
	}

	CStdioFile* pFile = NULL;

	char* buffer;
	buffer = new char[30000];
	
	pFile = pInternetSession->OpenURL(CString("http://www.my-url.com"));

	pFile->Read(buffer, 30000);

	ofstream myfile;
	myfile.open ("example.txt");
	myfile << buffer;
	myfile.close();


	pFile->Close();
	pInternetSession->Close();
	
}

Well, this is what I've tried so far, but as I told before no succes.

I would greatly appreciate it if anyone could point me in the right direction.


Greetz, Eddy

Dani AI

Generated

Short summary and focused troubleshooting notes to follow up on 's HttpSendRequest problem and 's working InternetOpenUrl approach.

First checklist (request parameters and environment). InternetConnect expects a server name (for example "www.example.com"), not a full URL with "http://", and the port parameter controls whether the default HTTP/HTTPS port is used — using the documented defaults avoids subtle mistakes. Also use InternetOpen so WinINet picks up the system proxy configuration when appropriate; wrong host/port or missed proxy settings are a frequent cause of a silent send-failure. InternetConnect docs (server/port), InternetOpen docs. (learn.microsoft.com)

How to get a concrete failure reason. When HttpSendRequest fails, call GetLastError immediately and then InternetGetLastResponseInfo to retrieve any extended WinINet/server text; use HttpQueryInfo to read response status or Location headers (redirects, 401/407 auth, SSL problems). Those APIs give actionable codes/messages to fix proxy, auth, or certificate issues instead of guessing. HttpSendRequest docs, InternetGetLastResponseInfo docs, HttpQueryInfo docs. (learn.microsoft.com)

Reading and robustness. Read in a loop until InternetReadFile indicates zero bytes left and append into a string or vector rather than assuming one call fills your buffer; handle large pages and check InternetQueryDataAvailable if you prefer to size buffers. For service-side code consider WinHTTP; for cross-platform work consider libcurl — both are well-documented alternatives. InternetReadFile docs, WinHTTP overview, libcurl. (learn.microsoft.com)

These checks will surface the real cause (bad host string/port, proxy/auth, SSL, or server redirect). They complement 's working example and help diagnose why 's HttpSendRequest path failed.

Recommended Answers

All 10 Replies

For your linker errors, you need to link with wininet.lib. Also, I don't think InternetReadFile null-terminates the data, so take that into account.

I dont know a lot about handling internet protocols using C++ and MFC but i think this should help you a bit.

Waiting for your feedback.
Bye.

This is what I've got so far. As you can see I added some error checking and the errors are at HttpSendRequest(hHttpFile, NULL, 0, NULL, 0); I'll post the full source here:

// openmic.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <stdio.h>
#include <iostream>
#include <windows.h>
#include <wininet.h>


int main(int argc, char* argv[])
{
	// Open Internet session.
HINTERNET hSession = ::InternetOpen("My app name",
                                    PRE_CONFIG_INTERNET_ACCESS,
                                    NULL, 
                                    INTERNET_INVALID_PORT_NUMBER,
                                    0) ;
if(!hSession){printf("Error while opening the internet session \n"); return 0;}

// Connect to www.microsoft.com.
HINTERNET hConnect = ::InternetConnect(hSession,
                                    "http://www.microsoft.com",
                                    INTERNET_INVALID_PORT_NUMBER,
                                    "",
                                    "",
                                    INTERNET_SERVICE_HTTP,
                                    0,
                                    0) ;
if(!hConnect){printf("Error while connecting to the server \n"); return 0;}
// Request the file /MSDN/MSDNINFO/ from the server.
HINTERNET hHttpFile = ::HttpOpenRequest(hConnect,
                                     NULL,
                                     "/MSDN/MSDNINFO/",
                                     HTTP_VERSION,
                                     NULL,
                                     0,
                                     INTERNET_FLAG_DONT_CACHE,
                                     0) ;
if(!hHttpFile){printf("Error while requesting the file \n"); return 0;}

// Send the request.
BOOL bSendRequest = ::HttpSendRequest(hHttpFile, NULL, 0, NULL, 0);

if(!bSendRequest){ printf("Error while sending request \n"); return 0;}

// Allocate a buffer for the file.   
char* buffer = new char[50000] ;

// Read the file into the buffer. 
DWORD dwBytesRead ;
BOOL bRead = ::InternetReadFile(hHttpFile,
                                buffer,
                                3000, 
                                &dwBytesRead);
// Put a zero on the end of the buffer.
buffer[dwBytesRead] = 0 ;

if(!bRead) buffer = "Nothing has been loaded";
// Close all of the Internet handles.
::InternetCloseHandle(hHttpFile); 
::InternetCloseHandle(hConnect) ;
::InternetCloseHandle(hSession) ;


printf("buffer: %s", buffer);
	return 0;
}

@everyone: thanks for your support, you've been very helpfull.


Greetz, Eddy

It would be better if you describe the error you get or print the error report here since all of us dont have Wininet library.

Yours truly,
~s.o.s~

Sorry if I didn't explain well. I'm not getting any errors while compiling or building. The buffer was just empty so I added error checking , like if(!bSendRequest){ printf("Error while sending request \n"); return 0;} now when I run the program I get "Error while sending request" as output to the command.

This made me think that somewhere went wrong at that function.


Greetz, Eddy

Try this instead of your stuff and see if this works.

// m_strURL => ur url
 
HINTERNET hINet, hFile;
hINet = InternetOpen("InetURL/1.0", INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0 );
if ( !hINet )
{
// bail out
}
hFile = InternetOpenUrl( hINet, "m_strURL, NULL, 0, 0, 0 );
if ( hFile )
{
CHAR buffer[1024];
DWORD dwRead;
while ( InternetReadFile( hFile, buffer, 1023, &dwRead ) )
{
if ( dwRead == 0 )
     break;
buffer[dwRead] = 0;
m_strURL += buffer;
}
InternetCloseHandle( hFile );
}
InternetCloseHandle( hINet );

huhhwwhaddayado?! (translation: Huh? What did you do?)

It works! It really does!

Thanks a million!


Greetz, Eddy

Glad i could help, after all this is why we are all here, right. ;)

And it would be really nice if you could post your entire working code along with everything so that it would be a good reference for other ppl.

You pretty much posted the whole code. I don't think it's usefull to post the full source of my program because I've got 250 lines so far (I don't know if comment is counted too, can anyone verify this?)

I guess that this project will be ~500 lines when it's finished.

Greetz,Eddy

Maybe as an attachment, when your project is finished so that a newbie can just copy and paste the code and understand its working. Of course if there are some other issues or reasons you dont want to post code then its ok.

Hoping to hear from you.

Regards,
~s.o.s~

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.