1

I have a C application (not C++ or C#) that tests connectivity to our API from a client's perspective using WinHTTP. Here's code that downloads a file.

 DWORD Measure(FILE* fout, LPCTSTR lpszUrl) {
      int i;
      DWORD dwResult = 0;
      DWORD dwSize = 0;
      DWORD dwDownloaded = 0;
      DWORD dwRequestSize = 0;
      LPSTR pszOutBuffer;
      BOOL  bResults = FALSE;
      HINTERNET  hSession = NULL;
      HINTERNET  hConnect = NULL;
      HINTERNET  hRequest = NULL;
      WINHTTP_STATUS_CALLBACK status = NULL;

      if (0 == dwResult) {
            hSession = WinHttpOpen(_T("Test/1.0"), WINHTTP_ACCESS_TYPE_DEFAULT_PROXY, WINHTTP_NO_PROXY_NAME, WINHTTP_NO_PROXY_BYPASS, 0);
            if (hSession == NULL) {
                 dwResult = GetLastError();
            }
      }

      if (0 == dwResult) {
            status = WinHttpSetStatusCallback(hSession, (WINHTTP_STATUS_CALLBACK)HttpCallback, WINHTTP_CALLBACK_FLAG_ALL_NOTIFICATIONS, NULL);
            if (WINHTTP_INVALID_STATUS_CALLBACK == status) {
                 dwResult = GetLastError();
            }
      }

      if (0 == dwResult) {
            hConnect = WinHttpConnect(hSession, lpszUrl, INTERNET_DEFAULT_HTTPS_PORT, 0);
            if (hConnect == NULL) {
                 dwResult = GetLastError();
            }
      }

      if (0 == dwResult) {
            hRequest = WinHttpOpenRequest(hConnect, L"GET",  NULL, NULL, WINHTTP_NO_REFERER, WINHTTP_DEFAULT_ACCEPT_TYPES, WINHTTP_FLAG_SECURE | WINHTTP_FLAG_REFRESH);
            if (hRequest == NULL) {
                 dwResult = GetLastError();
            }
      }

      if (0 == dwResult) {
            bResults = WinHttpSendRequest(hRequest,WINHTTP_NO_ADDITIONAL_HEADERS, 0, WINHTTP_NO_REQUEST_DATA, 0, 0, 0);
      }

      if (0 == dwResult) {
            if(!QueryPerformanceCounter(&liDownloadStart)){
                 dwResult = GetLastError();
            }
      }

      if (bResults) {
            bResults = WinHttpReceiveResponse(hRequest, NULL);
            if (!bResults) {
                 dwResult = GetLastError();
            }
      }

      if (dwResult == 0 && bResults) {
            do {
                 dwSize = 0;
                 if( !WinHttpQueryDataAvailable( hRequest, &dwSize ) ){
                      dwResult = GetLastError();
                 }

                 if (0 == dwResult) {
                      dwRequestSize += dwSize;
                 }

                 pszOutBuffer = (LPSTR)malloc(dwSize+1);
                 if (!pszOutBuffer) {
                      dwResult = ERROR_OUTOFMEMORY;
                 }

                 if (dwResult == 0) {
                      ZeroMemory(pszOutBuffer, dwSize + 1);
                      if( !WinHttpReadData( hRequest, (LPVOID)pszOutBuffer, dwSize, &dwDownloaded ) ){
                            dwResult = GetLastError();
                      }
                      free(pszOutBuffer);
                 }

            } while (dwSize > 0 && dwResult == 0);
      }

      if (hRequest) WinHttpCloseHandle(hRequest);
      if (hConnect) WinHttpCloseHandle(hConnect);
      if (hSession) WinHttpCloseHandle(hSession);

      return dwResult;
} 

I can enable tracing using the following module:

BOOL enable = TRUE;
WinHttpSetOption(NULL, WINHTTP_OPTION_ENABLETRACING, &enable, sizeof(enable));

Following the instructions in the article Capturing WinHTTP Logs, I could ask the user to enable tracing, including the path where files should be stored.

netsh winhttp set tracing trace-file-prefix="C:\Temp\dpws" level=verbose format=ansi state=enabled max-trace-file-size=1073741824

However, most folks running my application are technically challenged. They may forget to disable it when they are done.

How do I programmatically enable WinHTTP tracing for my process?

Cœur
  • 37,241
  • 25
  • 195
  • 267
bloudraak
  • 5,902
  • 5
  • 37
  • 52

0 Answers0