Hello All. Can anyone of you tell me why this piece of code doesn't work? the call to HttpOpenRequest() returns an 87 error code which is an invalid parameter ( returned by GetLastError() ) and i don't know what's causing it. please help.. thank you in advance. :)
Code:
#include "stdafx.h"
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <windows.h>
#include <wininet.h>
#include <strsafe.h>
#pragma comment (lib, "wininet.lib")
void ErrorExit(LPTSTR lpszFunction)
{
// Retrieve the system error message for the last-error code
LPVOID lpMsgBuf;
LPVOID lpDisplayBuf;
DWORD dw = GetLastError();
FormatMessage(
FORMAT_MESSAGE_ALLOCATE_BUFFER |
FORMAT_MESSAGE_FROM_SYSTEM |
FORMAT_MESSAGE_IGNORE_INSERTS,
NULL,
dw,
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
(LPTSTR) &lpMsgBuf,
0, NULL );
// Display the error message and exit the process
lpDisplayBuf = (LPVOID)LocalAlloc(LMEM_ZEROINIT,
(lstrlen((LPCTSTR)lpMsgBuf) + lstrlen((LPCTSTR)lpszFunction) + 40) * sizeof(TCHAR));
StringCchPrintf((LPTSTR)lpDisplayBuf,
LocalSize(lpDisplayBuf) / sizeof(TCHAR),
TEXT("%s failed with error %d: %s"),
lpszFunction, dw, lpMsgBuf);
MessageBox(NULL, (LPCWSTR)lpDisplayBuf, TEXT("Error"), MB_OK);
LocalFree(lpMsgBuf);
LocalFree(lpDisplayBuf);
ExitProcess(dw);
}
int Dumper(HINTERNET hResource)
{
LPTSTR lpszData; // buffer for the data
DWORD dwSize; // size of the data available
DWORD dwDownloaded; // size of the downloaded data
DWORD dwSizeSum=0; // size of the data in the textbox
LPTSTR lpszHolding; // buffer to merge the textbox data and buffer
LPTSTR textbox;
size_t size;
// This loop handles reading the data.
do
{
// The call to InternetQueryDataAvailable determines the
// amount of data available to download.
if (!InternetQueryDataAvailable(hResource,&dwSize,0,0))
{
printf("InternetQueryDataAvailable Error\n");
return FALSE;
}
else
{
// Allocate a buffer of the size returned by
// InternetQueryDataAvailable.
lpszData = new TCHAR[dwSize+1];
// Read the data from the HINTERNET handle.
if(!InternetReadFile(hResource,
(LPVOID)lpszData,
dwSize,
&dwDownloaded))
{
printf("InternetReadFile\n");
delete[] lpszData;
break;
}
else
{
// Add a null terminator to the end of the data buffer
lpszData[dwDownloaded]='\0';
// Allocate the holding buffer.
lpszHolding = new TCHAR[dwSizeSum + dwDownloaded + 1];
// Check if there has been any data written
// to the textbox.
if (dwSizeSum != 0)
{
lpszHolding = textbox;
// Add a null terminator at the end of the
// textbox data.
lpszHolding[dwSizeSum]='\0';
}
else
{
// Make the holding buffer an empty string.
lpszHolding[0]='\0';
}
size_t cchDest = dwSizeSum + dwDownloaded +
dwDownloaded + 1;
LPTSTR* ppszDestEnd;
size_t* pcchRemaining;
// Add the new data to the holding buffer
HRESULT hr = StringCchCatEx(lpszHolding,
cchDest,
lpszData,
ppszDestEnd,
pcchRemaining,
STRSAFE_NO_TRUNCATION);
if(SUCCEEDED(hr))
{
// Write the holding buffer to the textbox.
StringCchLength(lpszHolding,STRSAFE_MAX_CCH, &size );
StringCchCopy(textbox, size, lpszHolding );
// Delete the two buffers.
delete[] lpszHolding;
delete[] lpszData;
// Add the size of the downloaded data to the
// textbox data size.
dwSizeSum = dwSizeSum + dwDownloaded + 1;
// Check the size of the remaining data.
// If it is zero, break.
if (dwDownloaded == 0)
break;
}
else
{
// TODO: Insert error handling code here.
}
}
}
}while(TRUE);
// Close the HINTERNET handle.
InternetCloseHandle(hResource);
printf("%s",textbox);
// Return
return TRUE;
}
int _tmain(int argc, _TCHAR* argv[])
{
HINTERNET hOpen;
HINTERNET hConnect;
HINTERNET hRequest;
LPCWSTR ax = (LPCWSTR)"/index.php";
LPCWSTR bx = (LPCWSTR)"";
LPCWSTR httpVerb = (LPCWSTR)"GET" ;
const char* lplpszAcceptTypes[] = {"text/*", NULL};
hOpen = InternetOpen((LPCTSTR)"KimHttpGet",
INTERNET_OPEN_TYPE_DIRECT,
NULL,
NULL,
0 );
if ( NULL != hOpen )
{
hConnect = InternetConnect( hOpen,
(LPCWSTR)"www",
INTERNET_DEFAULT_HTTP_PORT,
NULL,
NULL,
INTERNET_SERVICE_HTTP,
0,
0 );
if ( NULL != hConnect )
{
hRequest = HttpOpenRequest( hConnect, //handle returned by InternetConnect
httpVerb, // POST or GET
ax, // file/object to get
HTTP_VERSION, // http version
NULL, //
(LPCWSTR*)lplpszAcceptTypes, //
INTERNET_FLAG_DONT_CACHE, // flags
1);
if ( NULL != hRequest )
{
if ( !HttpSendRequest( hRequest, NULL,0, NULL,0 ) )
{
Dumper(hRequest);
}
else
{
printf("HttpSendRequest Error\n");
ErrorExit((LPTSTR)"HttpSendRequest");
}
}
else
{
printf("HttpOpenRequest Error\n");
ErrorExit((LPTSTR)"HttpOpenRequest");
}
}
else
{
printf("InternetConnect Error\n");
ErrorExit((LPTSTR)"InternetConnect");
}
}
else
{
printf("InternetOpen Error\n");
ErrorExit((LPTSTR)"InternetOpen");
}
return 0;
}

