Sign in to follow this  
NecoSpes

Wininet Help

Recommended Posts

Ok, so when I run this, nothing happens. It's supposed to get the contents of google.com. :( Some of it I copied from this tutorial. I don't exactly get how dwRead is used here. I tried to look up what a DWORD was, and I only sort of get it. :(
#include <iostream>
#include <string>
#include <windows.h>
#include <wininet.h>
using namespace std;

int main()
{
	HINTERNET hInternet = InternetOpen("Microsoft Internet Explorer", INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, INTERNET_FLAG_ASYNC);
	if(!hInternet)
		cout << "error" << endl;
	HINTERNET hSite = InternetConnect(hInternet, "http://www.google.com/", 80, " "," ", INTERNET_SERVICE_HTTP, 0, 0);
	if(!hSite)
		cout << "error" << endl;
	CHAR buffer[2048];
	string contents;
	DWORD dwRead, dwFlags, dwStatus;
	HINTERNET hData = HttpOpenRequest( hSite, "GET", "", NULL, NULL, NULL, INTERNET_FLAG_KEEP_CONNECTION, 0 );
	if(!hData)
		cout << "error" << endl;
	HttpSendRequest( hData, NULL, 0, NULL, 0);
	while( InternetReadFile( hData, buffer, 255, &dwRead ) )
	{
		if ( dwRead == 0 )
			return 0;
		buffer[dwRead] = 0;
		cout << buffer << endl;
		contents += buffer;
	}
	
	InternetCloseHandle(hData);
	InternetCloseHandle(hSite);
	InternetCloseHandle(hInternet);
	return 0;
}


Thanks :)

Share this post


Link to post
Share on other sites
MSDN should be the first place you go to when you're doing anything Windows-related. Looking up DWORD there will bring you to a page telling you all of the custom types in the Windows API (which are mostly just typedef's of C primitives).

Share this post


Link to post
Share on other sites
http://www.google.co.uk didn't work, it still doesn't show anything.

I've already looked up all the functions used in MSDN, and it looks like it should work. I've tried using InternetOpenUrl before too, but that one didn't work either.

[source lang = "cpp"]
while( InternetReadFile( hData, buffer, 255, &dwRead ) )
{
if ( dwRead == 0 )
return 0;
buffer[dwRead] = 0;
cout << buffer << endl;
contents += buffer;
}


This segment of the code calls InternetReadFile until it has read all of the contents of hData, and has put it in buffer. But, what's the point of this line:
buffer[dwRead] = 0;

Share this post


Link to post
Share on other sites
It looks like the line of code adds a NULL to the end of the string that's been placed in buffer, so you can use it as a regular C-style NULL-terminated string. Since dwRead is the number of bytes read, if used as an index it gets you the element right after the last element written to by InternetReadFile.

The MSDN documentation says to use InternetGetLastResponseInfo and GetLastError to find out why the function fails. Have you done that yet?

Share this post


Link to post
Share on other sites
Yeah, each of the "error"s used to be GetLastError(), but nothing showed up.

I also put a GetLastError() after the while loop, and nothing showed up.

Oh...I just put one after the HttpSendRequest. I got the error code 997, which is: Overlapped I/O operation is in progress.

What does this mean?

Share this post


Link to post
Share on other sites
Ok, I fixed it by changing INTERNET_FLAG_ASYNC to 0.

Now my output comes out as "Bad Request" and says "Your browser made a request this server could not understand."

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this