使用WebClient登录和下载文件 [英] Using WebClient to login and download files
问题描述
我找到了执行此操作的不同示例,但是还无法将它们的任何组合发挥作用.
I've found different examples of doing this, but haven't been able to get any combination of them to work.
基本上,我有一个Intranet系统,可以从Web链接生成文档,并且知道要下载哪些文档.我可以生成要下载的链接列表,但是在程序中对系统进行身份验证时遇到问题.我与此一直收到401错误:
Basically, I have an intranet system that can generate documents from a web link, and I know which ones I want to download. I am able to generate the list of links I want to download, but I run into problems with authenticating to the system within the program. I keep getting a 401 error with this this:
Public Shared Sub DownloadFiles(_tool As Tool)
Dim links As List(Of String) = GetJiraLinks(_tool)
Dim downloader As New WebClient
' Initialize the client
Dim reqParm As New Specialized.NameValueCollection
reqParm.Add("os_username", "user")
reqParm.Add("os_password", "pass")
reqParm.Add("os_destination", "/secure/")
downloader.Credentials = New NetworkCredential("user", "pass")
Dim uploadLocation As String = My.Settings.jiraLocation & "login.jsp"
'downloader.Headers.Add("Content-Type", "application/x-www-form-urlencoded")
Dim responseBytes = downloader.UploadValues(uploadLocation, "POST", reqParm)
Dim responseBody = (New Text.UTF8Encoding).GetString(responseBytes)
Dim workingDir As String = CreateWorkingDir()
For Each link As String In links
Dim tempUri As New Uri(link)
Dim localpath As String = workingDir & "\" & System.IO.Path.GetFileName(tempUri.LocalPath)
downloader.DownloadFile(tempUri, localpath)
Next
End Sub
推荐答案
我认为我会发布一个完整的解决方案示例.还有许多其他文章链接到支持cookie的Web客户端(如何让WebClient使用Cookie?),但无法在操作中显示它.这是我的:
I figured I would post a fully working example of the solution. Many other posts link to a cookie aware web client (How can I get the WebClient to use Cookies?) but fail to show it in action. Here's mine:
Public Shared Sub DownloadFiles(_tool As Tool)
Dim links As List(Of String) = GetJiraLinks(_tool)
Dim downloader As New CookieAwareWebClient
' Start by requesting the page.
Dim loginPage As String = My.Settings.jiraLocation & "login.jsp"
' Initialize the client
Dim reqParm As New Specialized.NameValueCollection
reqParm.Add("os_username", "user")
reqParm.Add("os_password", "pass")
reqParm.Add("os_destination", "/secure/")
Dim responseBytes = downloader.UploadValues(loginPage, "POST", reqParm)
Dim responseBody = (New Text.UTF8Encoding).GetString(responseBytes)
Dim workingDir As String = CreateWorkingDir()
For Each link As String In links
Dim tempUri As New Uri(link)
Dim localpath As String = workingDir & "\" & System.IO.Path.GetFileName(tempUri.LocalPath)
downloader.DownloadFile(tempUri, localpath)
Next
End Sub
这篇关于使用WebClient登录和下载文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!