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?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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
Exploring SharePoint 2016

Explore SharePoint 2016, the web-based, collaborative platform that integrates with Microsoft Office to provide intranets, secure document management, and collaboration so you can develop your online and offline capabilities.

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
HappyCactusCommented:
Why not? Run the application within the debugger, or attach the debugger to the running process.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual C++.NET

From novice to tech pro — start learning today.