在定期我必須做在web瀏覽器手動如下:下載文件(第二部分)
- 轉到一個HTTPS的網站。
- 登錄網絡表單。
- 點擊鏈接下載一個大文件(135MB)。
我想使用.NET自動化這個過程。
幾天前我在這裏發佈了這個question這裏。感謝Rubens Farias的一段代碼,我現在可以執行上述步驟1和2.在步驟2之後,我可以讀取包含要下載文件的URL的頁面的HTML(使用afterLoginPage = reader .ReadToEnd())。如果登錄被授予,該頁面纔會顯示,因此步驟2被驗證爲成功。
我現在的問題是如何執行第3步。我嘗試了一些東西,但無濟於事,儘管之前登錄成功,文件的訪問被拒絕。
要澄清的事情,我會發佈下面的代碼,當然沒有實際的登錄信息和網站。最後,LoginPage後的變量包含登錄後頁面的HTML,包含我想下載的文件的鏈接。這個鏈接也以https開頭。
Dim httpsSite As String = "https://www.test.test/user/login"
' enter correct address
Dim formPage As String = ""
Dim afterLoginPage As String = ""
' Get postback data and cookies
Dim cookies As New CookieContainer()
Dim getRequest As HttpWebRequest = DirectCast(WebRequest.Create(httpsSite), HttpWebRequest)
getRequest.CookieContainer = cookies
getRequest.Method = "GET"
Dim wp As WebProxy = New WebProxy("[our proxies IP address]", [our proxies port number])
wp.Credentials = CredentialCache.DefaultCredentials
getRequest.Proxy = wp
Dim form As HttpWebResponse = DirectCast(getRequest.GetResponse(), HttpWebResponse)
Using response As New StreamReader(form.GetResponseStream(), Encoding.UTF8)
formPage = response.ReadToEnd()
End Using
Dim inputs As New Dictionary(Of String, String)()
inputs.Add("form_build_id", "[some code I'd like to keep secret]")
inputs.Add("form_id", "user_login")
For Each input As Match In Regex.Matches(formPage, "<input.*?name=""(?<name>.*?)"".*?(?:value=""(?<value>.*?)"".*?)? />", RegexOptions.IgnoreCase Or RegexOptions.ECMAScript)
If input.Groups("name").Value <> "form_build_id" And _
input.Groups("name").Value <> "form_id" Then
inputs.Add(input.Groups("name").Value, input.Groups("value").Value)
End If
Next
inputs("name") = "[our login name]"
inputs("pass") = "[our login password]"
Dim buffer As Byte() = Encoding.UTF8.GetBytes(_
[String].Join("&", _
Array.ConvertAll(Of KeyValuePair(Of String, String), String)(inputs.ToArray(), _
Function(item As KeyValuePair(Of String, String)) (item.Key & "=") + System.Web.HttpUtility.UrlEncode(item.Value))))
Dim postRequest As HttpWebRequest = DirectCast(WebRequest.Create(httpsSite), HttpWebRequest)
postRequest.CookieContainer = cookies
postRequest.Method = "POST"
postRequest.ContentType = "application/x-www-form-urlencoded"
postRequest.Proxy = wp
' send username/password
Using stream As Stream = postRequest.GetRequestStream()
stream.Write(buffer, 0, buffer.Length)
End Using
' get response from login page
Using reader As New StreamReader(postRequest.GetResponse().GetResponseStream(), Encoding.UTF8)
afterLoginPage = reader.ReadToEnd()
End Using
我已將此代碼轉換爲VB.NET,並且出現協議違規錯誤:「無法使用此動詞類型發送內容主體。」 Dim Stream As Stream = client.GetRequestStream() – George 2010-02-09 10:46:46
您應該直接獲取ResponseStream;如果你需要發送參數,使用查詢字符串 – 2010-02-09 11:58:05
沒關係,我沒有正確的轉換代碼。它現在有效,再次感謝。 – George 2010-02-09 12:39:12