<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
	<channel>
		<title><![CDATA[Серый форум &mdash; AHK:[function] URLDownloadToVar]]></title>
		<link>https://forum.script-coding.com/viewtopic.php?id=11183</link>
		<atom:link href="https://forum.script-coding.com/extern.php?action=feed&amp;tid=11183&amp;type=rss" rel="self" type="application/rss+xml" />
		<description><![CDATA[Недавние сообщения в теме «AHK:[function] URLDownloadToVar».]]></description>
		<lastBuildDate>Sun, 04 Feb 2018 18:41:01 +0000</lastBuildDate>
		<generator>PunBB</generator>
		<item>
			<title><![CDATA[Re: AHK:[function] URLDownloadToVar]]></title>
			<link>https://forum.script-coding.com/viewtopic.php?pid=123591#p123591</link>
			<description><![CDATA[<p>В переменную answer сохраняется результат выполнения функции UrlDownloadToVar.<br />Если вам не нужен разбор ответов от сервера, то можно сократить так:<br /></p><div class="codebox"><pre><code>Loop % i ;Request.MaxIndex()
	MsgBox % UrlDownloadToVar(Request[A_Index]*)</code></pre></div>]]></description>
			<author><![CDATA[null@example.com (Malcev)]]></author>
			<pubDate>Sun, 04 Feb 2018 18:41:01 +0000</pubDate>
			<guid>https://forum.script-coding.com/viewtopic.php?pid=123591#p123591</guid>
		</item>
		<item>
			<title><![CDATA[Re: AHK:[function] URLDownloadToVar]]></title>
			<link>https://forum.script-coding.com/viewtopic.php?pid=123553#p123553</link>
			<description><![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>]]></description>
			<author><![CDATA[null@example.com (DD)]]></author>
			<pubDate>Sat, 03 Feb 2018 03:58:25 +0000</pubDate>
			<guid>https://forum.script-coding.com/viewtopic.php?pid=123553#p123553</guid>
		</item>
		<item>
			<title><![CDATA[Re: AHK:[function] URLDownloadToVar]]></title>
			<link>https://forum.script-coding.com/viewtopic.php?pid=100087#p100087</link>
			<description><![CDATA[<p>Рабочий, просто там идет переадресация инсталлятора на github.</p>]]></description>
			<author><![CDATA[null@example.com (Malcev)]]></author>
			<pubDate>Fri, 01 Jan 2016 19:24:12 +0000</pubDate>
			<guid>https://forum.script-coding.com/viewtopic.php?pid=100087#p100087</guid>
		</item>
		<item>
			<title><![CDATA[Re: AHK:[function] URLDownloadToVar]]></title>
			<link>https://forum.script-coding.com/viewtopic.php?pid=100086#p100086</link>
			<description><![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>]]></description>
			<author><![CDATA[null@example.com (stealzy)]]></author>
			<pubDate>Fri, 01 Jan 2016 18:59:16 +0000</pubDate>
			<guid>https://forum.script-coding.com/viewtopic.php?pid=100086#p100086</guid>
		</item>
		<item>
			<title><![CDATA[Re: AHK:[function] URLDownloadToVar]]></title>
			<link>https://forum.script-coding.com/viewtopic.php?pid=100085#p100085</link>
			<description><![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>]]></description>
			<author><![CDATA[null@example.com (Malcev)]]></author>
			<pubDate>Fri, 01 Jan 2016 17:59:18 +0000</pubDate>
			<guid>https://forum.script-coding.com/viewtopic.php?pid=100085#p100085</guid>
		</item>
		<item>
			<title><![CDATA[AHK:[function] URLDownloadToVar]]></title>
			<link>https://forum.script-coding.com/viewtopic.php?pid=99989#p99989</link>
			<description><![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>]]></description>
			<author><![CDATA[null@example.com (stealzy)]]></author>
			<pubDate>Mon, 28 Dec 2015 03:44:46 +0000</pubDate>
			<guid>https://forum.script-coding.com/viewtopic.php?pid=99989#p99989</guid>
		</item>
	</channel>
</rss>
