0

我有一个需要从需要登录的站点下载的文件列表。除非设置了适当的 HttpRequestHeader.Cookie,否则不会下载该文件。

如果我手动登录该站点并通过提琴手拉出标题并将它们传递到 Cookie 插槽中,则文件会下载。我要做的是导航到站点(使用 Telerik 测试框架),登录,阅读我需要下载的 URL,然后调用 WebClient.DownloadFile。

我尝试通过 Telerik 下载文件,但在将作业部署到 Windows Server 后遇到了问题。

经过多次谷歌搜索,在导航到收集 URL 的页面后,我仍然无法弄清楚如何通过 Telerik 提取 HttpRequestHeaders - 这些应该是我需要传递给 WebClient 以下载文件的正确值.

有人可以指出我正确的方向或提供替代方案吗?

提前致谢。

        Using client
            client.Credentials = New NetworkCredential("user", "pass")
            client.Headers.Add(System.Net.HttpRequestHeader.Accept, "*/*")
            client.Headers.Add(System.Net.HttpRequestHeader.AcceptEncoding, "gzip, deflate")
            client.Headers.Add(System.Net.HttpRequestHeader.AcceptLanguage, "en-US")
            client.Headers.Add(System.Net.HttpRequestHeader.UserAgent, "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; Trident/4.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; InfoPath.3; .NET4.0C; .NET4.0E)")
            client.Headers.Add(System.Net.HttpRequestHeader.Cookie, "WLP-COOKIE1=r1593517685; JSESSIONID=Sz6G1lpvwhGB6FmqN6ySPbtf2cK2fs09hTtsX2sqcSwtLL7rKzpy!-1832769242; iPlanetDirectoryPro=AQIC5wM2LY4SfczHcm5%2FeHFKpWlxpQ71jtwzwJ75uHryjzA%3D%40AAJTSQACMTAAAlMxAAIwMQ%3D%3D%23; MMISPRODPOSCsource=AQIC5wM2LY4SfczHcm5%2FeHFKpWlxpQ71jtwzwJ75uHryjzA%3D%40AAJTSQACMTAAAlMxAAIwMQ%3D%3D%23")
            client.Headers.Add(System.Net.HttpRequestHeader.Referer, "https://mysite.com")
            client.DownloadFile(url, file)
        End Using
4

2 回答 2

1

我很久以前就为我编写了这个函数,它可以满足你的需要。尝试为您在 fiddler 中看到的每个重定向调用它,它会将所有响应 cookie 保存在全局cookie对象中。我已经从 c# 自动转换了这段代码,所以也许你需要稍微修改一下。

Private cookies As CookieContainer = Nothing
Public Function GetPOST(url As String, data As String) As String
    Dim httpWebRequest__1 As HttpWebRequest = Nothing
    Try
        httpWebRequest__1 = DirectCast(HttpWebRequest.Create(New Uri(url)), HttpWebRequest)

        httpWebRequest__1.Method = If([String].IsNullOrWhiteSpace(data), WebRequestMethods.Http.[Get], WebRequestMethods.Http.Post)
        httpWebRequest__1.ContentType = "application/x-www-form-urlencoded"
        httpWebRequest__1.UserAgent = _userAgent
        httpWebRequest__1.Accept = "*/*"
        httpWebRequest__1.Headers.Add(HttpRequestHeader.AcceptLanguage, "de-DE,de;q=0.8,en-US;q=0.7,en;q=0.3")
        httpWebRequest__1.Headers.Add(HttpRequestHeader.AcceptCharset, "ISO-8859-1,utf-8;q=0.7,*;q=0.3")
        httpWebRequest__1.Headers.Add(HttpRequestHeader.AcceptEncoding, "gzip,deflate,sdch")
        httpWebRequest__1.Headers.Add("X-Requested-With", "XMLHttpRequest")
        httpWebRequest__1.AutomaticDecompression = DecompressionMethods.GZip Or DecompressionMethods.Deflate

        httpWebRequest__1.Timeout = InlineAssignHelper(httpWebRequest__1.ReadWriteTimeout, 15000)
        httpWebRequest__1.CookieContainer = cookies

        If Not [String].IsNullOrWhiteSpace(data) Then
            Dim dataBytes As Byte() = ASCIIEncoding.[Default].GetBytes(data)
            httpWebRequest__1.ContentLength = dataBytes.Length
            Using stream As Stream = httpWebRequest__1.GetRequestStream()
                stream.Write(dataBytes, 0, dataBytes.Length)
            End Using
        End If

        If httpWebRequest__1.CookieContainer Is Nothing Then
            httpWebRequest__1.CookieContainer = New CookieContainer()
        End If

        Dim res As HttpWebResponse = DirectCast(httpWebRequest__1.GetResponse(), HttpWebResponse)
        Using stream As Stream = res.GetResponseStream()
            Dim str As New StreamReader(stream)
            _currentResponceLocation = res.ResponseUri.ToString()
            Return str.ReadToEnd()
        End Using
    Catch generatedExceptionName As Exception
        Return GetPOST(url, data)
    Finally
        For Each c As Cookie In httpWebRequest__1.CookieContainer.GetCookies(New Uri("https://" + httpWebRequest__1.Host))
            cookies.SetCookies(New Uri("https://" + httpWebRequest__1.Host), c.Name + "=" + c.Value)
        Next
    End Try
End Function
于 2013-11-01T20:11:36.160 回答
0

终于偶然发现了这一点,通过 Telerik 很简单。我只是将此函数的结果传递到 webclient 的 cookie 标头中:

Public Function GetCookies(ByVal url As String) As String

    Dim cookies As String

    For Each cookie In xBrowser.Cookies.GetCookies(url)
        If String.IsNullOrEmpty(cookies) Then
            cookies &= cookie.ToString
        Else
            cookies &= "; " & cookie.ToString
        End If
    Next

    Return cookies

End Function
于 2013-11-05T18:02:35.987 回答