<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">
	<title type="html"><![CDATA[Серый форум &mdash; AHK:[function] URLDownloadToVar]]></title>
	<link rel="self" href="https://forum.script-coding.com/extern.php?action=feed&amp;tid=11183&amp;type=atom" />
	<updated>2018-02-04T18:41:01Z</updated>
	<generator>PunBB</generator>
	<id>https://forum.script-coding.com/viewtopic.php?id=11183</id>
		<entry>
			<title type="html"><![CDATA[Re: AHK:[function] URLDownloadToVar]]></title>
			<link rel="alternate" href="https://forum.script-coding.com/viewtopic.php?pid=123591#p123591" />
			<content type="html"><![CDATA[<p>В переменную answer сохраняется результат выполнения функции UrlDownloadToVar.<br />Если вам не нужен разбор ответов от сервера, то можно сократить так:<br /></p><div class="codebox"><pre><code>Loop % i ;Request.MaxIndex()
	MsgBox % UrlDownloadToVar(Request[A_Index]*)</code></pre></div>]]></content>
			<author>
				<name><![CDATA[Malcev]]></name>
				<uri>https://forum.script-coding.com/profile.php?id=26930</uri>
			</author>
			<updated>2018-02-04T18:41:01Z</updated>
			<id>https://forum.script-coding.com/viewtopic.php?pid=123591#p123591</id>
		</entry>
		<entry>
			<title type="html"><![CDATA[Re: AHK:[function] URLDownloadToVar]]></title>
			<link rel="alternate" href="https://forum.script-coding.com/viewtopic.php?pid=123553#p123553" />
			<content type="html"><![CDATA[<p>Как записать эту часть кода <br /></p><div class="codebox"><pre><code>
MsgBox,, % ((answer:=UrlDownloadToVar(Request[A_Index]*))=false  ?  &quot;Error:&quot;  :  &quot;Success:&quot;) &quot; &quot; (URL:=Request[A_Index][1]), % answer=false ? ErrorLevel : answer
</code></pre></div><p>в виде —<br /></p><div class="codebox"><pre><code>
zzzzz = ((answer:=UrlDownloadToVar(Request[A_Index]*))=false  ?  &quot;Error:&quot;  :  &quot;Success:&quot;) &quot; &quot; (URL:=Request[A_Index][1]), % answer=false ? ErrorLevel : answer
MsgBox, %zzzzz%
</code></pre></div><p>Чтобы в MsgBox была только переменная?</p>]]></content>
			<author>
				<name><![CDATA[DD]]></name>
				<uri>https://forum.script-coding.com/profile.php?id=25263</uri>
			</author>
			<updated>2018-02-03T03:58:25Z</updated>
			<id>https://forum.script-coding.com/viewtopic.php?pid=123553#p123553</id>
		</entry>
		<entry>
			<title type="html"><![CDATA[Re: AHK:[function] URLDownloadToVar]]></title>
			<link rel="alternate" href="https://forum.script-coding.com/viewtopic.php?pid=100087#p100087" />
			<content type="html"><![CDATA[<p>Рабочий, просто там идет переадресация инсталлятора на github.</p>]]></content>
			<author>
				<name><![CDATA[Malcev]]></name>
				<uri>https://forum.script-coding.com/profile.php?id=26930</uri>
			</author>
			<updated>2016-01-01T19:24:12Z</updated>
			<id>https://forum.script-coding.com/viewtopic.php?pid=100087#p100087</id>
		</entry>
		<entry>
			<title type="html"><![CDATA[Re: AHK:[function] URLDownloadToVar]]></title>
			<link rel="alternate" href="https://forum.script-coding.com/viewtopic.php?pid=100086#p100086" />
			<content type="html"><![CDATA[<p>Спасибо за наводку на тему. Код в сообщении однако нерабочий. Буду разбираться.<br />_______________<br />UPD:<br />Понял из-за чего ошибки.<br />Гитхаб не отдает заголовок Content-Length отдельно. Только все заголовки вместе. Заголовок Accept-Ranges не отдает вовсе (ну и не надо, поставим Exception).<br />Но это для zip. Если говорить про ссылку из сообщения (&quot;http://ahkscript.org/download/ahk-install.exe&quot;), то там даже Content-Length нету.</p>]]></content>
			<author>
				<name><![CDATA[stealzy]]></name>
				<uri>https://forum.script-coding.com/profile.php?id=31937</uri>
			</author>
			<updated>2016-01-01T18:59:16Z</updated>
			<id>https://forum.script-coding.com/viewtopic.php?pid=100086#p100086</id>
		</entry>
		<entry>
			<title type="html"><![CDATA[Re: AHK:[function] URLDownloadToVar]]></title>
			<link rel="alternate" href="https://forum.script-coding.com/viewtopic.php?pid=100085#p100085" />
			<content type="html"><![CDATA[<div class="quotebox"><blockquote><p>Если знаете, как загрузить только первые Х байт файла, вообще было бы отлично.</p></blockquote></div><p><a href="http://forum.script-coding.com/viewtopic.php?pid=83761#p83761">http://forum.script-coding.com/viewtopi … 761#p83761</a></p>]]></content>
			<author>
				<name><![CDATA[Malcev]]></name>
				<uri>https://forum.script-coding.com/profile.php?id=26930</uri>
			</author>
			<updated>2016-01-01T17:59:18Z</updated>
			<id>https://forum.script-coding.com/viewtopic.php?pid=100085#p100085</id>
		</entry>
		<entry>
			<title type="html"><![CDATA[AHK:[function] URLDownloadToVar]]></title>
			<link rel="alternate" href="https://forum.script-coding.com/viewtopic.php?pid=99989#p99989" />
			<content type="html"><![CDATA[<p>Альтернатива команде URLDownloadToFile.<br />Обработка ошибок, таймауты, печеньки, прокси, referrer, user-agent...<br />В случае успеха возращает содержимое URL,<br /> в случае ошибки возращает false и пишет причину ошибки в ErrorLevel.</p><p>Функция с примером:<br /></p><div class="codebox"><pre><code>Request:={}, i:=0
Request[++i] := [&quot;http://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js&quot;]
Request[++i] := [&quot;http://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js&quot;,, timeout:=0.3]  ; too little time
; Request[++i] := [&quot;http://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js&quot;,, timeout:=1.5]
Request[++i] := [&quot;http://ajax.google-shmoogle.com&quot;]  ; Non-exist domain
Request[++i] := [&quot;http://ajax.googleapis.com/ho-ho!&quot;]  ; Not found - 404
Request[++i] := [&quot;http://www.tingchina.com/play/yousheng/flash.asp?id=24366&amp;inum=1&amp;flei=恐怖惊悚&amp;bookname=我当阴曹官的那几年&amp;filename=001.mp3&amp;rand=16&amp;nexturl=play_24366_1.htm&quot;] ; not so easy to get this page
Request[++i] := [&quot;http://www.tingchina.com/play/yousheng/flash.asp?id=24366&amp;inum=1&amp;flei=恐怖惊悚&amp;bookname=我当阴曹官的那几年&amp;filename=001.mp3&amp;rand=16&amp;nexturl=play_24366_1.htm&quot;, Referer:=&quot;http://www.tingchina.com/yousheng/24366/play_24366_0.htm&quot;]
Request[++i] := [&quot;http://www.tingchina.com/play/yousheng/flash.asp?id=24366&amp;inum=1&amp;flei=恐怖惊悚&amp;bookname=我当阴曹官的那几年&amp;filename=001.mp3&amp;rand=16&amp;nexturl=play_24366_1.htm&quot;, Referer:=&quot;http://www.tingchina.com/yousheng/24366/play_24366_0.htm&quot;,,,,,,,, Charset:=&quot;gb2312&quot;]

Loop % i ;Request.MaxIndex()
	MsgBox,, % ((answer:=UrlDownloadToVar(Request[A_Index]*))=false  ?  &quot;Error:&quot;  :  &quot;Success:&quot;) &quot; &quot; (URL:=Request[A_Index][1]), % answer=false ? ErrorLevel : answer
ExitApp
Esc::ExitApp
Return

UrlDownloadToVar(URL, Referer=&quot;&quot;, TimeoutSec=-1, UserAgent=&quot;&quot;, Cookie=&quot;&quot;, Proxy=&quot;&quot;, ProxyBypassList=&quot;&quot;, EnableRedirects=&quot;&quot;, URLCodePage=&quot;&quot;, Charset=&quot;&quot;) {
	; If something wrong - return false, put explanation in ErrorLevel
	; autors: stealzy, tuzi
	; More about WinHttpRequest: https://msdn.microsoft.com/en-us/library/windows/desktop/aa383979%28v=vs.85%29.aspx

	WebRequest := ComObjCreate(&quot;WinHttp.WinHttpRequest.5.1&quot;)
	t := URLCodePage ? WebRequest.Option(2):=URLCodePage :
	t := (EnableRedirects &lt;&gt; &quot;&quot;) ? WebRequest.Option(6):=EnableRedirects :
	t := Proxy ? WebRequest.SetProxy(2,Proxy,ProxyBypassList) :
	; WebRequest.SetTimeouts(ResolveTimeout:=0, ConnectTimeout:=60000, SendTimeout:=30000, ReceiveTimeout:=30000) ; time in ms
	; https://msdn.microsoft.com/library/windows/desktop/aa384061%28v=vs.85%29.aspx
	try WebRequest.Open(&quot;GET&quot;, URL, true)
		catch	Error {
			ErrorLevel := &quot;Wrong URL format&quot;
			return false
		}

	t := Cookie ? WebRequest.SetRequestHeader(&quot;Cookie&quot;, Cookie) :
	t := Referer ? WebRequest.SetRequestHeader(&quot;Referer&quot;, Referer) :
	t := UserAgent ? WebRequest.SetRequestHeader(&quot;User-Agent&quot;, UserAgent) :
	WebRequest.Send()
	t := A_TickCount
	try Suc:=WebRequest.WaitForResponse(TimeoutSec+0) ; Timeout in seconds; without &quot;+0&quot; interpreted as string
		catch	Error {
			OutputDebug % &quot;Error WaitForResponse: &quot; A_TickCount - t
			ErrorLevel := &quot;No internet access / No existing domain&quot;
			return false
		}
	OutputDebug % &quot;Success WaitForResponse: &quot; A_TickCount - t 	; if no internet access, ≈ 21-23 sec
	try HTTPStatusCode := WebRequest.Status
		catch	Error {
			ErrorLevel := &quot;WebRequest.Status not ready = timeout expired&quot;
			return false
		}
	if (SubStr(HTTPStatusCode, 1, 1) ~= &quot;4|5&quot;) { ; 4xx — Client Error, 5xx — Server Error. wikipedia.org/wiki/List_of_HTTP_status_codes
		ErrorLevel := &quot;Error HTTP Status Code: &quot; HTTPStatusCode
		return false
	}

	If (Charset=&quot;&quot;) {
		try ResponseText := WebRequest.ResponseText()
			catch	Error {
				ErrorLevel := &quot; WebRequest.ResponseText not ready = timeout expired&quot;
				return false
			}
	} Else {
		ADO := ComObjCreate(&quot;adodb.stream&quot;)  
		ADO.Type := 1 
		ADO.Mode := 3
		ADO.Open() 
		ADO.Write(WebRequest.ResponseBody())  
		ADO.Position := 0
		ADO.Type := 2 
		ADO.Charset := Charset    
		ResponseText := ADO.ReadText()   
	}
	return ResponseText
}</code></pre></div><p>Если знаете наглядные примеры зависимости отдаваемого контента от юзер-агента или куков - присылайте, добавлю к примерам.<br />Если знаете, как загрузить только первые Х байт файла, вообще было бы отлично.</p>]]></content>
			<author>
				<name><![CDATA[stealzy]]></name>
				<uri>https://forum.script-coding.com/profile.php?id=31937</uri>
			</author>
			<updated>2015-12-28T03:44:46Z</updated>
			<id>https://forum.script-coding.com/viewtopic.php?pid=99989#p99989</id>
		</entry>
</feed>
