如何下载带有cookie和URL的文件?

时间:2017-12-31 19:12:54

标签: vb.net file cookies download automation

我自动从网站下载.xlsx文件时遇到大问题。 我已经尝试了以下代码并准备好了cookie:

Dim request As HttpWebRequest = CType(WebRequest.Create(("http://www.trademap.org/Country_SelProduct_TS.aspx?nvpm=1|||||0101|||4|1|1|1|2|1|2|1|1")), HttpWebRequest)
request.CookieContainer = New CookieContainer()
Dim response As HttpWebResponse = CType(request.GetResponse(), HttpWebResponse)
For Each cook As Cookie In response.Cookies
msgbox(cook.value)
Next

我有cookie,但不知道如何使用cookie和URL下载文件而不被问到。可以通过webclient.downloadfile或类似的东西来完成吗?

1 个答案:

答案 0 :(得分:0)

您的第二个请求必须使用相同的CookieContainer对象来共享其信息。

Dim cookieContainer as New CookierContainer()

Dim request As HttpWebRequest = CType(WebRequest.Create(("http://www.trademap.org/Country_SelProduct_TS.aspx?nvpm=1|||||0101|||4|1|1|1|2|1|2|1|1")), HttpWebRequest)
request.CookieContainer = cookieContainer 
Dim response As HttpWebResponse = CType(request.GetResponse(), HttpWebResponse)

Dim request2 as HttpWebRequest = CType(WebRequest.Create("second url"), HttpWebRequest);
request2.CookieContainer = cookieContainer
Dim response2 As HttpWebResponse = CType(request2.GetResponse(), HttpWebResponse)