C++ WINHTTP Web Page Download

Hi,

Im having problems with a segment of code. its purpose is to download the contents of an SSL web page and return its contents from the function. whilst it runs without error, it is actually not returning anything. by adding periodic return codes, i can see that it is getting as far as the second if(bresults) which says to me that winhhtpreceiveresponse is not getting any data, which must mean its a problem with the code above that, but im stumped if i can see why. ideas?

please ignore any return("xxx"); these are just in there to see how far it was getting
string update_html()
{
  //DWORD dwSize = 0;
  //DWORD dwDownloaded = 0;
  
  std::string strRet;
  LPSTR			pszOutBuffer = NULL;
  BOOL  bResults = FALSE;
  LPVOID			lpOutBuffer = NULL;
  bool ssl = true;

  HINTERNET  hSession = NULL, 
             hConnect = NULL,
             hRequest = NULL;

  // Use WinHttpOpen to obtain a session handle.
  hSession = WinHttpOpen( L"WinHTTP Example/1.0",  
                          WINHTTP_ACCESS_TYPE_DEFAULT_PROXY,
                          WINHTTP_NO_PROXY_NAME, 
                          WINHTTP_NO_PROXY_BYPASS, 0 );

  // Specify an HTTP server.
  if( hSession )
    hConnect = WinHttpConnect( hSession, L"www.4xtechnology.com",
                               INTERNET_DEFAULT_HTTPS_PORT, 0 );

  // Create an HTTP request handle.
  if( hConnect )
    hRequest = WinHttpOpenRequest( hConnect, L"GET",
								   L"/services/copier/status/4D924305-D140-DF11-BD24-0022192534A9.html",
								   NULL, WINHTTP_NO_REFERER,
								   WINHTTP_DEFAULT_ACCEPT_TYPES,
								   (ssl) ? WINHTTP_FLAG_SECURE : 0);

  // Send a request.
  if( hRequest )
    bResults = WinHttpSendRequest( hRequest,
                                   WINHTTP_NO_ADDITIONAL_HEADERS, 0,
                                   WINHTTP_NO_REQUEST_DATA, 0, 
                                   0, 0 );

	DWORD dwFlags = SECURITY_FLAG_IGNORE_CERT_CN_INVALID | 
						SECURITY_FLAG_IGNORE_CERT_DATE_INVALID | 
						SECURITY_FLAG_IGNORE_UNKNOWN_CA;

		// important - set security to ignore 'bad' certs
		bResults = WinHttpSetOption(hRequest,
									WINHTTP_OPTION_SECURITY_FLAGS,
									&dwFlags,
									sizeof(DWORD));

  // End the request.
  if (bResults)
	{
		
		bResults = WinHttpReceiveResponse(hRequest, NULL);
	}
				
	// First, use WinHttpQueryHeaders to obtain the size of the buffer.
	DWORD	dwSize = sizeof(DWORD);
	
	if (bResults)
	{
		WinHttpQueryHeaders(	hRequest,
								WINHTTP_QUERY_RAW_HEADERS_CRLF,
								WINHTTP_HEADER_NAME_BY_INDEX,
								NULL,
								&dwSize,
								WINHTTP_NO_HEADER_INDEX);

		// 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_HEADER_NAME_BY_INDEX,
											lpOutBuffer,
											&dwSize,
											WINHTTP_NO_HEADER_INDEX);

		} else if (GetLastError() == ERROR_WINHTTP_CLIENT_AUTH_CERT_NEEDED)
			// TODO: SSL
		{
			// FIXME: unknown error
		}
	return("kitten2");
	}
		
		// Free the allocated memory.
		delete [] lpOutBuffer;
					
		DWORD	dwDownloaded = 0;
	// Fetch page body
	
	if (bResults)
	{
		do
		{
			// Verify available data.
			dwSize = 0;
			if (!WinHttpQueryDataAvailable( hRequest, &dwSize))
			{
			}
	
			// Allocate space for the buffer.
			pszOutBuffer = new char[dwSize + 1];
			
			if (!pszOutBuffer)
			{
				dwSize = 0;
			}
			else
			{
				// Read the Data.
				ZeroMemory(pszOutBuffer, dwSize + 1);
		
				if (!WinHttpReadData( hRequest, (LPVOID)pszOutBuffer, dwSize, &dwDownloaded))
				{
					delete [] pszOutBuffer;
					
					if (hRequest) WinHttpCloseHandle(hRequest);
					if (hConnect) WinHttpCloseHandle(hConnect);
					if (hSession) WinHttpCloseHandle(hSession);
					return "9";
				} 
				else
				{
					strRet = pszOutBuffer;
					delete [] pszOutBuffer;
					
					if (hRequest) WinHttpCloseHandle(hRequest);
					if (hConnect) WinHttpCloseHandle(hConnect);
					if (hSession) WinHttpCloseHandle(hSession);

					return strRet; 
				}

			}


		} while (dwSize>0);
		return("cde");
	}

	// Close any open handles.
	delete [] pszOutBuffer;
	
	if (hRequest) WinHttpCloseHandle(hRequest);
	if (hConnect) WinHttpCloseHandle(hConnect);
	if (hSession) WinHttpCloseHandle(hSession);
return("abcc");

Open in new window

GreatSampsAsked:
Who is Participating?
 
HappyCactusCommented:
Why not? Run the application within the debugger, or attach the debugger to the running process.
0
 
HappyCactusCommented:
I think you should handle also the error condition by inspecting the GetLastError() codes. This way you can understand why the function fails.
0
 
DanRollinsCommented:
Place a breakpoint in the code and single-step through it.  Examine variables as you go.
That, by the way, is how to solve ALL programming problems :-)
0
Cloud Class® Course: Microsoft Office 2010

This course will introduce you to the interfaces and features of Microsoft Office 2010 Word, Excel, PowerPoint, Outlook, and Access. You will learn about the features that are shared between all products in the Office suite, as well as the new features that are product specific.

 
GreatSampsAuthor Commented:
Hi,

thanks for your responses. The application is actually a DLL, which is called by another app, so i believe you cant use a debugger in this instance?
0
 
GreatSampsAuthor Commented:
Agreed, using the debugger is way to resolve the problem, and thanks for letting me know you can still use them with DLL's
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.