title: VBA: Download a picture via HTTPS
body: <p>I'm trying to download a picture from a website (Grafana monitoring tool, API) using VBA (MS Access 2016). It's a HTTPS address using a self signed ceritificat and requires username and password for authentication. </p>
  
  <p>I'm currently using the following code to download the picture:</p>
  
  <pre><code>Function DownloadFile(ByVal myURL As String, ByVal LocalFileName As String) 
      Dim WinHttpReq As Object
      Set WinHttpReq = CreateObject("WinHttp.WinHttpRequest.5.1")
      WinHttpReq.Option(4) = 13056          'Ignore SSL Errors
      WinHttpReq.Open "GET", myURL, False
      WinHttpReq.setRequestHeader "Accept", "*/*"
      WinHttpReq.setRequestHeader "User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64)"
      WinHttpReq.setRequestHeader "Proxy-Connection", "Keep-Alive"
      WinHttpReq.Send
  
      myURL = WinHttpReq.ResponseBody
      If WinHttpReq.Status = 200 Then
         Set oStream = CreateObject("ADODB.Stream")
         oStream.Open
         oStream.Type = 1
         oStream.Write WinHttpReq.ResponseBody
         oStream.SaveToFile LocalFileName, 2
         oStream.Close
      Else
         MsgBox "Returncode:" &amp; WinHttpReq.Status &amp; " Unable to download picture."
      End If
  End function
  </code></pre>
  
  <p>If I paste the HTTPS-URL into my browser I get the right picture. This picture I would like to have as a PNG file downloaded by the code above. But instead of the picture I get the following HTML code downloaded:</p>
  
  <pre><code>&lt;!DOCTYPE html&gt;
  &lt;html lang="en"&gt;
    &lt;head&gt;
  &lt;script&gt;
    !(function() {
      if ('PerformanceLongTaskTiming' in window) {
        var g = (window.__tti = { e: [] });
        g.o = new PerformanceObserver(function(l) {
          g.e = g.e.concat(l.getEntries());
        });
        g.o.observe({ entryTypes: ['longtask'] });
      }
    })();
  &lt;/script&gt;
  &lt;meta charset="utf-8" /&gt;
  &lt;meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" /&gt;
  &lt;meta name="viewport" content="width=device-width" /&gt;
  &lt;meta name="theme-color" content="#000" /&gt;
  
  &lt;title&gt;Grafana&lt;/title&gt;
  &lt;base href="/" /&gt;
  
  &lt;link
    rel="preload"
    href="public/fonts/roboto/RxZJdnzeo3R5zJw.woff2"
    as="font"
    crossorigin
  /&gt;
  
  &lt;link rel="icon" type="image/png" href="public/img/fav32.png" /&gt;
  &lt;link rel="apple-touch-icon" sizes="180x180" href="public/img/apple-touch-icon.png" /&gt;
  &lt;link rel="mask-icon" href="public/img/grafana_mask_icon.svg" color="#F05A28" /&gt;
  &lt;link rel="stylesheet" href="public/build/grafana.dark.4d0490af40c.css" /&gt;
  
  &lt;script&gt;
    performance.mark('css done blocking');
  &lt;/script&gt;
  &lt;meta name="apple-mobile-web-app-capable" content="yes" /&gt;
  &lt;meta name="apple-mobile-web-app-status-bar-style" content="black" /&gt;
  &lt;meta name="msapplication-TileColor" content="#2b5797" /&gt;
  &lt;meta name="msapplication-config" content="public/img/browserconfig.xml" /&gt;
    &lt;/head&gt;
    &lt;body class="theme-dark app-grafana"&gt;
  &lt;style&gt;
    .preloader {
      height: 100%;
      flex-direction: column;
      display: flex;
      justify-content: center;
      align-items: center;
    }
  
    .preloader__enter {
      opacity: 0;
      animation-name: preloader-fade-in;
      animation-iteration-count: 1;
      animation-duration: 0.9s;
      animation-delay: 1.35s;
      animation-fill-mode: forwards;
    }
  
    .preloader__bounce {
      text-align: center;
      animation-name: preloader-bounce;
      animation-duration: 0.9s;
      animation-iteration-count: infinite;
    }
  
    .preloader__logo {
      display: inline-block;
      animation-name: preloader-squash;
      animation-duration: 0.9s;
      animation-iteration-count: infinite;
      width: 60px;
      height: 60px;
      background-repeat: no-repeat;
      background-size: contain;
      background-image: url("data:image/svg+xml,%3csvg version='1.1' id='Layer_1' xmlns='http://www.w3.org/2000/svg' xmlns:xlink='http://www.w3.org/1999/xlink' x='0px' y='0px' width='351px' height='365px' viewBox='0 0 351 365' style='enable-background:new 0 0 351 365%3b' xml:space='preserve'%3e %3cstyle type='text/css'%3e .st0%7bfill:url(%23SVGID_1_)%3b%7d %3c/style%3e %3cg id='Layer_1_1_'%3e %3c/g%3e %3clinearGradient id='SVGID_1_' gradientUnits='userSpaceOnUse' x1='175.5' y1='445.4948' x2='175.5' y2='114.0346'%3e %3cstop offset='0' style='stop-color:%23FFF100'/%3e %3cstop offset='1' style='stop-color:%23F05A28'/%3e %3c/linearGradient%3e %3cpath class='st0' d='M342%2c161.2c-0.6-6.1-1.6-13.1-3.6-20.9c-2-7.7-5-16.2-9.4-25c-4.4-8.8-10.1-17.9-17.5-26.8 c-2.9-3.5-6.1-6.9-9.5-10.2c5.1-20.3-6.2-37.9-6.2-37.9c-19.5-1.2-31.9%2c6.1-36.5%...2c1.6%2c2.3%2c3%2c2.1c1.5-0.1%2c2.6-1.3%2c2.6-2.8C342.6%2c170.4%2c342.5%2c166.1%2c342%2c161.2z'/%3e %3c/svg%3e");
    }
  
    .preloader__text {
      margin-top: 16px;
      font-weight: 500;
      font-size: 14px;
      font-family: Sans-serif;
      opacity: 0;
      animation-name: preloader-fade-in;
      animation-duration: 0.9s;
      animation-delay: 1.8s;
      animation-fill-mode: forwards;
    }
  
    .preloader__text--fail {
      display: none;
    }
  
    .preloader--done .preloader__bounce,
    .preloader--done .preloader__logo {
      animation-name: none;
      display: none;
    }
  
    .preloader--done .preloader__logo,
    .preloader--done .preloader__text {
      display: none;
      color: #ff5705 !important;
      font-size: 15px;
    }
  
    .preloader--done .preloader__text--fail {
      display: block;
    }
  
    [ng\:cloak],
    [ng-cloak],
    .ng-cloak {
      display: none !important;
    }
  &lt;/style&gt;
  
  &lt;div class="preloader"&gt;
    &lt;div class="preloader__enter"&gt;
      &lt;div class="preloader__bounce"&gt;
        &lt;div class="preloader__logo"&gt;&lt;/div&gt;
      &lt;/div&gt;
    &lt;/div&gt;
    &lt;div class="preloader__text"&gt;Loading Grafana&lt;/div&gt;
    &lt;div class="preloader__text preloader__text--fail"&gt;
      &lt;p&gt;
        &lt;strong&gt;If you're seeing this Grafana has failed to load its application files&lt;/strong&gt;
        &lt;br /&gt;
        &lt;br /&gt;
      &lt;/p&gt;
      &lt;p&gt;
        1. This could be caused by your reverse proxy settings.&lt;br /&gt;&lt;br /&gt;
        2. If you host grafana under subpath make sure your grafana.ini root_url setting includes subpath&lt;br /&gt;
        &lt;br /&gt;
        3. If you have a local dev build make sure you build frontend using: yarn start, yarn start:hot, or yarn
        build&lt;br /&gt;
        &lt;br /&gt;
        4. Sometimes restarting grafana-server can help&lt;br /&gt;
      &lt;/p&gt;
    &lt;/div&gt;
  &lt;/div&gt;
  
  &lt;grafana-app class="grafana-app" ng-cloak&gt;
    &lt;sidemenu class="sidemenu"&gt;&lt;/sidemenu&gt;
    &lt;app-notifications-list class="page-alert-list"&gt;&lt;/app-notifications-list&gt;
    &lt;dashboard-search&gt;&lt;/dashboard-search&gt;
  
    &lt;div class="main-view"&gt;
              &lt;div ng-view class="scroll-canvas"&gt;&lt;/div&gt;
    &lt;/div&gt;
  &lt;/grafana-app&gt;
  
  &lt;script&gt;
      window.grafanaBootData = {
        user: {"isSignedIn":false,"id":0,"login":"","email":"","name":"","lightTheme":false,"orgCount":0,"orgId":0,"orgName":"","orgRole":"","isGrafanaAdmin":false,"gravatarUrl":"","timezone":"browser","locale":"en-US","helpFlags1":0,"hasEditPermissionInFolders":false},
        settings: {"alertingEnabled":true,"alertingErrorOrTimeout":"alerting","alertingMinInterval":1,"alertingNoDataOrNullValues":"no_data","allowOrgCreate":false,"appSubUrl":"","authProxyEnabled":false,"buildInfo":{"buildstamp":1582200229,"commit":"3fa63cfc34","edition":"Open Source","env":"production","hasUpdate":false,"isEnterprise":false,"latestVersion":"","version":"6.6.2"},"datasources":{"-- Dashboard --":{"meta":{"type":"datasource","name":"--...plugins","url":"/plugins"}]},{"id":"help","text":"Help","subTitle":"Grafana v6.6.2 (3fa63cfc34)","icon":"gicon gicon-question","url":"#","sortWeight":-1200,"hideFromMenu":true}]
      };
  
      window.onload = function() {
        var preloader = document.getElementsByClassName("preloader");
        if (preloader.length) {
          preloader[0].className = "preloader preloader--done";
        }
      };
  &lt;/script&gt;
  
  &lt;script src="public/build/runtime.4d0490a94b199a11f40c.js" type="text/javascript"&gt;&lt;/script&gt;
  &lt;script src="public/build/angular~app.4d0490a94b199a11f40c.js" type="text/javascript"&gt;&lt;/script&gt;
  &lt;script src="public/build/app.4d0490a94b199a11f40c.js" type="text/javascript"&gt;&lt;/script&gt;
  &lt;script src="public/build/moment~app.4d0490a94b199a11f40c.js" type="text/javascript"&gt;&lt;/script&gt;
  &lt;script src="public/build/vendors~app.4d0490a94b199a11f40c.js" type="text/javascript"&gt;&lt;/script&gt;
  &lt;script&gt;
    performance.mark('js done blocking');
  &lt;/script&gt;
  </code></pre>
  
  <p>
  </p>
  
  <p>Does anybody know how I can solve this problem in VBA?</p>