Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/vb.net/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
.net 使用WebClient登录和下载文件_.net_Vb.net_Webclient_Webclient Download - Fatal编程技术网

.net 使用WebClient登录和下载文件

.net 使用WebClient登录和下载文件,.net,vb.net,webclient,webclient-download,.net,Vb.net,Webclient,Webclient Download,我已经找到了很多这样做的例子,但是没有任何一种组合能够起作用 基本上,我有一个内联网系统,可以从web链接生成文档,我知道要下载哪些文档。我能够生成要下载的链接列表,但在程序内对系统进行身份验证时遇到问题。我一直收到一个401错误: Public Shared Sub DownloadFiles(_tool As Tool) Dim links As List(Of String) = GetJiraLinks(_tool) Dim downloader As New WebC

我已经找到了很多这样做的例子,但是没有任何一种组合能够起作用

基本上,我有一个内联网系统,可以从web链接生成文档,我知道要下载哪些文档。我能够生成要下载的链接列表,但在程序内对系统进行身份验证时遇到问题。我一直收到一个401错误:

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客户端(),但没有在实际操作中显示它。这是我的:

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没有保存此cookie。我找到了,但它没有显示如何使用它。