webclient in autohotkey [英] Webclient in autohotkey

查看:103
本文介绍了webclient in autohotkey的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这有效:

This works:

URLDownloadToFile, https://www.nseindia.com/content/historical/EQUITIES/2016/JAN/cm01JAN2016bhav.csv.zip, C:\Users\Dave\Desktop\Happy.zip



但我不明白为什么即使URL语法或格式相同也不会这样:


But I donot understand why this does not even if the URL syntax or format is same:

URLDownloadToFile, https://www.nseindia.com/content/historical/EQUITIES/2015/DEC/cm31DEC2015bhav.csv.zip, C:\Users\Dave\Desktop\Sad.zip





我的尝试:



我发现以下用C#编写的方法来自 social.msdn 但我的AHK技能非常有限。你能帮忙将下面的C#转换成AHK吗?



What I have tried:

I found the below method written in C# from social.msdn for the same issue but my AHK skills are very limited. Could you help convert the below C# to AHK?

WebClient objWebClient = new WebClient();
Uri uriWebClient = new Uri(url);
objWebClient.Headers.Add("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)");
objWebClient.Headers.Add("Content-Type", "application / zip, application / octet - stream");
objWebClient.Headers.Add("Accept-Encoding", "gzip,deflate,sdch");
objWebClient.Headers.Add("Referer", "http://Something");
objWebClient.Headers.Add("Accept","text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8");
objWebClient.DownloadFile(uriWebClient, filepath);

推荐答案

因为第二个URL返回 403:禁止



响应状态意味着即使您通过了身份验证,也不允许您下载该文件。



HTTP状态代码列表 - 维基百科 [ ^ ]

HTTP 403 - 维基百科 [ ^ ]



编辑:看起来服务器正在验证引荐来源标头。将其更改为:

Because the second URL returns 403: Forbidden.

That response status means you are not allowed to download that file, even if you were authenticated.

List of HTTP status codes - Wikipedia[^]
HTTP 403 - Wikipedia[^]

Looks like the server is validating the referrer header. Change it to:
objWebClient.Headers.Add("Referer", "https://www.nseindia.com/products/content/equities/equities/archieve_eq.htm");


谢谢Richard,

我发现这个 StackOverflow页面

使用下载方法和您的建议,能够获得理想的结果。

Thanks Richard,
I found this StackOverflow page.
Using the Download method and your suggestion, able to get the desired outcome.
Download( UrlToFile, SaveFileAs:="", Overwrite := True, headers := "", method := "GET", postData := "")
{
	 MsgBox, %headers%
     WinHttpObj := ComObjCreate( "WinHttp.WinHttpRequest.5.1" )
     WinHttpObj.Open( method, UrlToFile )
     For header, value in headers 
         WinHttpObj.SetRequestHeader( header, value )
     WinHttpObj.Send( postData )

     ADODBObj := ComObjCreate( "ADODB.Stream" )
     ADODBObj.Type := 1
     ADODBObj.Open()
     ADODBObj.Write( WinHttpObj.ResponseBody )
     If !SaveFileAs
	 {
         urlSplitArray := StrSplit( UrlToFile, "/" )
         SaveFileAs := urlSplitArray[ urlSplitArray.MaxIndex() ]
     }        
     ADODBObj.SaveToFile( SaveFileAs, Overwrite ? 2:1 )
     ADODBObj.Close()
}

customHeaders := { "User-Agent": "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)"
				 ,"Content-Type": "application / zip, application / octet - stream""application / zip, application / octet - stream"
				 ,"Accept-Encoding": "gzip,deflate,sdch"
				 ,"Referer": "https://www.nseindia.com/products/content/equities/equities/archieve_eq.htm"
				 ,"Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8" }

Download( "https://www.nseindia.com/content/historical/EQUITIES/2015/DEC/cm31DEC2015bhav.csv.zip", "C:\Users\Dave\Desktop\Happy.zip", True, customHeaders )


这篇关于webclient in autohotkey的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆