title: Download picture through xmlhttp
body: <p>I am trying to get the src attribute of pictures on a page, then to download that picture and here's my code</p>
  <pre><code>picURL = &quot;https://iofferman.x.yupoo.com/33269655?uid=1&quot;
  
  Set htmlPic = GetHTML(picURL)
  Debug.Print Replace(htmlPic.querySelector(&quot;.viewer__imgwrap img&quot;).getAttribute(&quot;src&quot;), &quot;about:&quot;, &quot;https:&quot;)
  Dim myPic As String
  myPic = Replace(htmlPic.querySelector(&quot;.viewer__imgwrap img&quot;).getAttribute(&quot;src&quot;), &quot;about:&quot;, &quot;https:&quot;)
  'URLDownloadToFile 0, myPic, ThisWorkbook.Path &amp; &quot;\&quot; &amp; picID &amp; &quot;.jpg&quot;, 0, 0
  Call SaveWebFile(myPic, ThisWorkbook.Path &amp; &quot;\&quot; &amp; picID &amp; &quot;.jpg&quot;)
  </code></pre>
  <p>This is related to code</p>
  <pre><code>Function GetHTML(ByVal sURL As String) As HTMLDocument
      Dim http As MSXML2.XMLHTTP60, html As MSHTML.HTMLDocument
      Set http = New MSXML2.XMLHTTP60
      Set html = New MSHTML.HTMLDocument
      With http
          .Open &quot;Get&quot;, sURL, False
          .send
          html.body.innerHTML = .responseText
      End With
      Set GetHTML = html
  End Function
  </code></pre>
  <p>and this is the UDF for downloading the picture</p>
  <pre><code>Function SaveWebFile(ByVal sFile$, ByVal sPath$) As Boolean
      Dim f&amp;, oResp() As Byte
      With CreateObject(&quot;MSXML2.ServerXMLHTTP&quot;)
          .Open &quot;GET&quot;, sFile, False
          .send
          Do While (.readyState &lt;&gt; 4): DoEvents: Loop
          oResp = .responseBody
      End With
      f = FreeFile
      If Dir(sPath) &lt;&gt; &quot;&quot; Then Kill sPath
      Open sPath For Binary As #f
      Put #f, , oResp
      Close #f
  End Function
  </code></pre>
  <p>I could get the link for the picture, for example: <code>https://photo.yupoo.com/iofferman/2d03c9b8/326e8e47.jpg</code> but when navigating to it, I sometimes could get the picture correctly and sometimes gives me incorrect output like that <code>http://adc.yupoo.com/res/703.gif</code>. How can I download the picture correctly?</p>