Friday, August 12, 2011

Uploading chunk data to server using winhttp

DWORD cbWritten=0,cbData=100;


////////////////////////////////
UCHAR* szFileBuf= NULL;
long bytes=0;
const char filename[] = "c:\\PrintJob.txt"; //IPP_Content.txt";
FILE *file;
struct stat fileinfo;
if (filename != NULL)
{
if (stat(filename, &fileinfo))
{
LPVOID lpMsgBuf;
FormatMessage( FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, NULL, GetLastError(),
0, (LPTSTR) &lpMsgBuf, 0, NULL );

::MessageBox( NULL, (LPCTSTR)lpMsgBuf, L"dfsd", MB_OK | MB_ICONINFORMATION );
return ;
}
if ((file = fopen(filename, "rb")) == NULL)
{
return ;
}

rewind(file);
szFileBuf = new UCHAR[(size_t)fileinfo.st_size];
bytes = fread(szFileBuf, 1, (size_t)fileinfo.st_size, file);

if (bytes<=0) return ;

if (file)
fclose(file);
}




DWORD dwSize = 0;
DWORD dwDownloaded = 0;
LPSTR pszOutBuffer;
BOOL bResults = FALSE;
HINTERNET hSession = NULL,
hConnect = NULL,
hRequest = NULL;

DWORD dwStatusCode = 0;
DWORD dwSupportedSchemes=0;
DWORD dwFirstScheme=0;
DWORD dwTarget=0;

hSession = WinHttpOpen( L"WinHTTP Example/1.0",
WINHTTP_ACCESS_TYPE_DEFAULT_PROXY,
WINHTTP_NO_PROXY_NAME,
WINHTTP_NO_PROXY_BYPASS, 0);

if (hSession)
hConnect = WinHttpConnect( hSession, L"16.182.218.240",
631, 0); //INTERNET_DEFAULT_HTTP_PORT


CString str;
str += L"Content-Type: application/ipp\r\n";
str += L"HOST: 16.182.218.240:631\r\n";
str += L"Transfer-Encoding: chunked\r\n";

if (hConnect)
hRequest = WinHttpOpenRequest( hConnect, L"POST",
L"ipp/printer",// cs1,//urlComp.lpszUrlPath,
NULL, WINHTTP_NO_REFERER,
WINHTTP_DEFAULT_ACCEPT_TYPES,
WINHTTP_FLAG_REFRESH);



if (hRequest)
bResults = WinHttpSendRequest( hRequest, str, -1,WINHTTP_NO_REQUEST_DATA, 0,WINHTTP_IGNORE_REQUEST_TOTAL_LENGTH, NULL);
// bResults = WinHttpSendRequest( hRequest, str, -1,(LPVOID)szFileBuf, bytes,bytes, 0);

int k = GetLastError();

if (!bResults)
{
LPVOID lpMsgBuf;
FormatMessage( FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, NULL, GetLastError(),
0, (LPTSTR) &lpMsgBuf, 0, NULL );
::MessageBox( NULL, (LPCTSTR)lpMsgBuf, L"dfsd", MB_OK | MB_ICONINFORMATION );

}

char szHttpChunkHeaderA[32];
char szHttpChunkFooterA[] = "\r\n";
StringCchPrintfA(szHttpChunkHeaderA,ARRAYSIZE(szHttpChunkHeaderA),"%X\r\n",bytes );

cbWritten = 0;
bResults = WinHttpWriteData(hRequest, szHttpChunkHeaderA,(DWORD)( strlen(szHttpChunkHeaderA) * sizeof(char) ),&cbWritten );

if (!bResults)
{
LPVOID lpMsgBuf;
FormatMessage( FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, NULL, GetLastError(),
0, (LPTSTR) &lpMsgBuf, 0, NULL );
::MessageBox( NULL, (LPCTSTR)lpMsgBuf, L"dfsd", MB_OK | MB_ICONINFORMATION );

}

cbWritten = 0;
bResults = WinHttpWriteData( hRequest, (LPVOID)szFileBuf, bytes, &cbWritten );


cbWritten = 0;
bResults = WinHttpWriteData(
hRequest,
szHttpChunkFooterA,
(DWORD)( strlen(szHttpChunkFooterA) * sizeof(char) ),
&cbWritten
);

if (!bResults)
{
LPVOID lpMsgBuf;
int y = GetLastError();
FormatMessage( FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, NULL, GetLastError(),
0, (LPTSTR) &lpMsgBuf, 0, NULL );
::MessageBox( NULL, (LPCTSTR)lpMsgBuf, L"dfsd", MB_OK | MB_ICONINFORMATION );

}
cbWritten = 0 ;
bResults = WinHttpWriteData( hRequest, "0\r\n",(DWORD)( strlen("0\r\n") * sizeof(char)), &cbWritten );


// bResults = WinHttpWriteData(hRequest,szHttpChunkFooterA,(DWORD)( strlen(szHttpChunkFooterA) * sizeof(char) ),&cbWritten );

if (!bResults)
{
LPVOID lpMsgBuf;
FormatMessage( FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, NULL, GetLastError(),
0, (LPTSTR) &lpMsgBuf, 0, NULL );
::MessageBox( NULL, (LPCTSTR)lpMsgBuf, L"dfsd", MB_OK | MB_ICONINFORMATION );

}

cbWritten = 0;
bResults = WinHttpWriteData(
hRequest,
szHttpChunkFooterA,
(DWORD)( strlen(szHttpChunkFooterA) * sizeof(char) ),
&cbWritten
);
if( !bResults )
{
return;
}


// End the request.
if (bResults)
bResults = WinHttpReceiveResponse( hRequest, NULL);
CString respage=L""; // The buffer that'll contain the
// extracted Web page data


delete[] szFileBuf;

if (!bResults)
{
LPVOID lpMsgBuf;
FormatMessage( FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, NULL, GetLastError(),
0, (LPTSTR) &lpMsgBuf, 0, NULL );
::MessageBox( NULL, (LPCTSTR)lpMsgBuf, L"dfsd", MB_OK | MB_ICONINFORMATION );

}


int h = GetLastError();

/* bResults = WinHttpQueryAuthSchemes( hRequest,
&dwSupportedSchemes,
&dwFirstScheme,
&dwTarget );*/





h = GetLastError();

if (bResults)
{
WCHAR *lpOutBuffer = NULL;
WinHttpQueryHeaders( hRequest, WINHTTP_QUERY_RAW_HEADERS_CRLF ,
WINHTTP_HEADER_NAME_BY_INDEX, NULL,
&dwSize, WINHTTP_NO_HEADER_INDEX);

int b =GetLastError( );

// Allocate memory for the buffer.
if( GetLastError( ) == ERROR_INSUFFICIENT_BUFFER )
{
lpOutBuffer = new WCHAR[dwSize/sizeof(WCHAR)];

// Now, use WinHttpQueryHeaders to retrieve the header.
bResults = WinHttpQueryHeaders( hRequest,
WINHTTP_QUERY_RAW_HEADERS_CRLF, // WINHTTP_QUERY_STATUS_CODE
WINHTTP_HEADER_NAME_BY_INDEX,
(LPVOID)lpOutBuffer, &dwSize,
WINHTTP_NO_HEADER_INDEX);

RetrieveHeader(lpOutBuffer,dwSize);

b =GetLastError( );
}

if (lpOutBuffer)
delete[] lpOutBuffer;
}



// Keep checking for data until there is nothing left.
if (bResults)
do
{

// Check for available data.
dwSize = 0;
if (!WinHttpQueryDataAvailable( hRequest, &dwSize))
printf("Error %u in WinHttpQueryDataAvailable.\n",
GetLastError());

// Allocate space for the buffer.
pszOutBuffer = new char[dwSize+1];
if (!pszOutBuffer)
{
printf("Out of memory\n");
dwSize=0;
}
else
{
// Read the Data.
ZeroMemory(pszOutBuffer, dwSize+1);

if (!WinHttpReadData( hRequest,
(LPVOID)pszOutBuffer,
dwSize, &dwDownloaded))
printf("Error %u in WinHttpReadData.\n",
GetLastError());
else
{
CString csData(pszOutBuffer);
respage += csData;
}

// Free the memory allocated to the buffer.
delete [] pszOutBuffer;
}

} while (dwSize>0);

AfxMessageBox(respage);

1 comment:

SkipLife said...

Hello,

I tried running your code on Windows 2008 R2. WinHTTPWriteData call fails on the second time with error 12030 while the communication happens over SSL (443). I was wondering if you would know what needs to be done to make this go through?