简体   繁体   English

获取HTTP请求头VB.Net/Telerik测试框架

[英]Get HTTP Request Headers VB.Net / Telerik Testing Framework

I have a list of files that I need to download from a site that requires a login. 我有一个文件列表,需要从需要登录的站点下载。 The file will not download unless the appropriate HttpRequestHeader.Cookie is set. 除非设置了适当的HttpRequestHeader.Cookie,否则该文件将不会下载。

If I login to the site manually and pull the headers through fiddler and pass these into the Cookie slot, the file downloads. 如果我手动登录该站点,并通过提琴手提取标题并将其传递到Cookie插槽,则会下载文件。 What I'm trying to do is navigate to the site (using Telerik Testing Framework), login, read the URLs I need to download and then call the WebClient.DownloadFile. 我想做的是导航到该站点(使用Telerik Testing Framework),登录,阅读我需要下载的URL,然后调用WebClient.DownloadFile。

I attempted to download the files through Telerik but was having issues once I deployed the job onto Windows Server. 我试图通过Telerik下载文件,但是将作业部署到Windows Server时出现了问题。

After much googling, I still can't figure out how I can pull the HttpRequestHeaders through Telerik after I navigate to the page where I'm gathering the URLs - these should be the correct values I need to pass into the WebClient to download the file. 经过大量的搜索之后,在导航到收集URL的页面后,我仍然不知道如何通过Telerik拉HttpRequestHeaders-这些应该是我需要传递给WebClient来下载文件的正确值。

Can somebody point me in the right direction or offer an alternative? 有人可以指出我正确的方向还是提供其他选择?

Thanks in advance. 提前致谢。

        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

I have written this function a long time ago for me and it does what you need. 我很久以前已经为我编写了此函数,它可以满足您的需求。 Try to call it for every redirect which you see in fiddler and it will save all response cookies in the global cookie object. 尝试为您在fiddler中看到的每个重定向调用它,它将所有响应cookie保存在全局cookie对象中。 I've auto converted this this code from c#, so maybe you need to modify it a little. 我已经从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

Finally stumbled upon this and it was rather simple through Telerik. 最终偶然发现了这一点,通过Telerik进行操作相当简单。 I just pass the result of this function into the the cookie header for the webclient: 我只是将此函数的结果传递到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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM