<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">
	<title type="html"><![CDATA[Серый форум &mdash; AHK: Надёжное скачивание изображения]]></title>
	<link rel="self" href="https://forum.script-coding.com/extern.php?action=feed&amp;tid=15898&amp;type=atom" />
	<updated>2020-12-08T09:41:42Z</updated>
	<generator>PunBB</generator>
	<id>https://forum.script-coding.com/viewtopic.php?id=15898</id>
		<entry>
			<title type="html"><![CDATA[AHK: Надёжное скачивание изображения]]></title>
			<link rel="alternate" href="https://forum.script-coding.com/viewtopic.php?pid=144001#p144001" />
			<content type="html"><![CDATA[<p>Есть задача надёжно качать картинку — задействовать условие, чтобы скачивание происходило только когда изображение действительно доступно. В реальном примере ссылка на изображение бывает активна только по определенным условиям (логин, включение доступа и тп), хотя наверно это относится и к любым изображениям. То есть, как скачивать с учётом разрывов соединений, сбросов логина — только когда известно, что это картинка?</p><br /><div class="codebox"><pre><code>
HTTP := ComObjCreate(&quot;WinHTTP.WinHTTPRequest.5.1&quot;)
User_Agent := &quot;Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.79 Safari/537.36&quot;

   loop  ;CONNECT_LOOP
   {     ;CONNECT_LOOP
      HTTP.Open(&quot;GET&quot;, URL, true)
      HTTP.SetRequestHeader(&quot;Referer&quot;, URL)
      ;HTTP.SetRequestHeader(&quot;Cookie&quot;, cookies)
      ;HTTP.SetRequestHeader(&quot;Cookie2&quot;, &quot;$Version=1&quot;)
      HTTP.SetRequestHeader(&quot;Accept-Language&quot;, &quot;ru-RU,ru;q=0.9,en-US;q=0.8,en;q=0.7&quot;)
      HTTP.SetRequestHeader(&quot;User-Agent&quot;, User_Agent)
      HTTP.SetRequestHeader(&quot;Pragma&quot;, &quot;no-cache&quot;)
      HTTP.SetRequestHeader(&quot;If-Modified-Since&quot;, &quot;Sat, 1 Jan 2000 00:00:00 GMT&quot;)

      try {
         HTTP.Send()
         HTTP.WaitForResponse()
         ;!!MsgBox, % HTTP.StatusText 
         ResponseHeaders := HTTP.getAllResponseHeaders()
         Body := HTTP.ResponseBody
/*
         status := HTTP.status
         if status in 404
            break
*/
         If InStr(ResponseHeaders, &quot;Content-Type:&quot;) and not InStr(ResponseHeaders, &quot;image/jpeg&quot;)
            break
         pData := NumGet(ComObjValue(Body), A_PtrSize = 8? 16:12, &quot;ptr&quot;)
         File := FileOpen(A_ScriptDir &quot;\&quot; page_code &quot;\&quot; ren, &quot;w&quot;)
         File.RawWrite(pData + 0, Body.MaxIndex() + 1)

         break
      }
      catch {
         Sleep, 5000
      }
   }     ;CONNECT_LOOP
</code></pre></div>]]></content>
			<author>
				<name><![CDATA[DD]]></name>
				<uri>https://forum.script-coding.com/profile.php?id=25263</uri>
			</author>
			<updated>2020-12-08T09:41:42Z</updated>
			<id>https://forum.script-coding.com/viewtopic.php?pid=144001#p144001</id>
		</entry>
</feed>
