21.03.2013 Views

Problem - Kevin Tafuro

Problem - Kevin Tafuro

Problem - Kevin Tafuro

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

DWORD dwHeadersLength = 0;<br />

LPSTR lpszOptional = 0;<br />

DWORD dwOptionalLength = 0;<br />

bResult = HttpSendRequest(hRequest, lpszHeaders, dwHeadersLength, lpOptional,<br />

dwOptionalLength);<br />

After sending the request, the server’s response can be retrieved. As part of sending<br />

the request, WinInet will retrieve the response headers from the server. Information<br />

about the response can be obtained using the HttpQueryInfo( ) function. A complete<br />

list of the information that may be available can be found in the WinInet documentation,<br />

but for our purposes, the only information we’re concerned with is the content<br />

length. The server is not required to send a content length header back as part of its<br />

response, so we must also be able to handle the case where it is not sent. Response<br />

data sent by the server after its response headers can be obtained by calling<br />

InternetReadFile( ) as many times as necessary to retrieve all of the data.<br />

DWORD dwContentLength, dwIndex, dwInfoLevel;<br />

DWORD dwBufferLength, dwNumberOfBytesRead, dwNumberOfBytesToRead;<br />

LPVOID lpBuffer, lpFullBuffer, lpvBuffer;<br />

dwInfoLevel = HTTP_QUERY_CONTENT_LENGTH;<br />

lpvBuffer = (LPVOID)&dwContentLength;<br />

dwBufferLength = sizeof(dwContentLength);<br />

dwIndex = 0;<br />

HttpQueryInfo(hRequest, dwInfoLevel, lpvBuffer, &dwBufferLength, &dwIndex);<br />

if (dwIndex != ERROR_HTTP_HEADER_NOT_FOUND) {<br />

/* Content length is known. Read only that much data. */<br />

lpBuffer = GlobalAlloc(GMEM_FIXED, dwContentLength);<br />

InternetReadFile(hRequest, lpBuffer, dwContentLength, &dwNumberOfBytesRead);<br />

} else {<br />

/* Content length is not known. Read until EOF is reached. */<br />

dwContentLength = 0;<br />

dwNumberOfBytesToRead = 4096;<br />

lpFullBuffer = lpBuffer = GlobalAlloc(GMEM_FIXED, dwNumberOfBytesToRead);<br />

while (InternetReadFile(hRequest, lpBuffer, dwNumberOfBytesToRead,<br />

&dwNumberOfBytesRead)) {<br />

dwContentLength += dwNumberOfBytesRead;<br />

if (dwNumberOfBytesRead != dwNumberOfBytesToRead) break;<br />

lpFullBuffer = GlobalReAlloc(lpFullBuffer, dwContentLength +<br />

dwNumberOfBytesToRead, 0);<br />

lpBuffer = (LPVOID)((LPBYTE)lpFullBuffer + dwContentLength);<br />

}<br />

lpFullBuffer = lpBuffer = GlobalReAlloc(lpFullBuffer, dwContentLength, 0);<br />

}<br />

After the data has been read with InternetReadFile( ), the variable lpBuffer will hold<br />

the contents of the server’s response, and the variable dwContentLength will hold the<br />

number of bytes contained in the response data buffer. At this point, the request has<br />

been completed, and the request object should be destroyed by calling<br />

InternetCloseHandle( ). If additional requests to the same connection are required, a<br />

Securing Web Communication on Windows Using the WinInet API | 467<br />

This is the Title of the Book, eMatter Edition<br />

Copyright © 2007 O’Reilly & Associates, Inc. All rights reserved.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!