Home
Tutorials
Code Snippets
Code Samples
Downloads
Links

The Blog
Our Projects
About
Contact

::Add RageStorm to Favorites!::

The Blog | Our Projects | Guest Book | About | Contact

 
Code Snippet - WinInet API
Author:Arkon
Category:Internet & Networks
File Size:~ 1.94 KB
Uploaded at:10-Nov-06 02:19:13 pm
Description:
  Shows how to download a file over the Inet using the WinInet API.
  
#include <windows.h>
#pragma comment (lib, "Wininet.lib")

#include <wininet.h>
int main(int argc, char* argv[])
{


#define MAXFILESIZE (0x10000)
unsigned char incomingFile[MAXFILESIZE] = {0};
HINTERNET hRequest = NULL, hi = NULL, hConnection = NULL, hurl = NULL;

hi = InternetOpen("RageStorm", INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0);
if (!hi) {
printf("can't open connection");
return 0;
}

hConnection = InternetConnect(hi, "63.247.128.207", INTERNET_DEFAULT_HTTP_PORT, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 0);
if (hConnection == NULL) {
printf("error connecting to site");
goto end;
}

// OpenRequest is useful because you can specify headers yourself...
hRequest = HttpOpenRequest(hConnection, "GET", "/index.php", NULL, NULL, NULL, INTERNET_FLAG_RELOAD | INTERNET_FLAG_EXISTING_CONNECT, 0);
if (hRequest == NULL) {
printf("error with request");
goto end;
}

if (!HttpSendRequest(hRequest, NULL, 0, 0, 0)) {
printf("request wasn't sent, %d!", GetLastError());
goto end;
}

// If you want you can use this API instead the request...
/*hurl = InternetOpenUrl(hi, "http://www.ragestorm.net/", NULL, -1, INTERNET_FLAG_RELOAD, 0);
if (!hurl) {
char buf[256] = {0};
DWORD e = 0, l = 256;
InternetGetLastResponseInfo(&e, buf, &l);
printf("inet error: %u, %s     ", e, buf);
printf("can't open url: %d\r\n", GetLastError());
goto end;
}*/

DWORD bytesDownloaded = -1, count = 0;
while (bytesDownloaded != 0) {
if (!InternetReadFile((hurl == NULL) ? hRequest : hurl, &incomingFile[count], MAXFILESIZE - count, &bytesDownloaded)) {
printf("can't download file, %d", GetLastError());
goto end;
}
count += bytesDownloaded;
}
printf("%s", incomingFile);

end:
// clean ups
if (hi) InternetCloseHandle(hi);
if (hConnection) InternetCloseHandle(hConnection);
if (hRequest) InternetCloseHandle(hRequest);
if (hurl) InternetCloseHandle(hurl);

return 0;
}
User Contributed Comments(None)
NOTE:
Comments that will hurt anyone in any way will be deleted.
Don't ask for features, advertise or curse.
If you want to leave a message to the author use the contacts,
if you have any question in relation to your comments please use the forum.
Comments which violate any of these requests will be deleted without further
notice. Use the comment system decently.

Post your comment:
Name:
email:
Comment:
::Top 5 Tutorials::
Embedded Python[105776]
2D Rotated Rectangles Collision Detection[79024]
Keyboard Hook[68594]
UDP[52667]
HTTP Proxy[35944]

::Top 5 Samples::
2D ColDet Rotated Rectangles[10589]
PS2 Mouse Driver[6083]
Wave Format Player[4968]
Reading FAT12[4855]
CodeGuru[4543]


All rights reserved to RageStorm © 2009