none
webdav en dotNet

Réponses

Toutes les réponses

  • MEs sources :

            'Get Url and Port
            Dim url As String = Me.txtUrl.Text.Trim()
            Dim port As String = Me.txtPort.Text.Trim()
            'If the port was provided, then insert it into the url.
            If port <> "" Then
                'Insert the port into the Url
                'https://www.example.com:80/fileToDownload.dat
                Dim u As New Uri(url)
                'Get the host (example: www.example.com)
                Dim host As String = u.Host
                'Replace the host with the host:port
                url = url.Replace(host, host & ":" & port)
            End If
            'Create the request
            Dim request As HttpWebRequest = _
                DirectCast(System.Net.HttpWebRequest.Create(url), HttpWebRequest)
            'Set the User Name and Password
            request.Credentials = New NetworkCredential(Me.txtUserName.Text.Trim(), Me.txtPassword.Text.Trim())
            request.PreAuthenticate = True
            request.Method = WebRequestMethods.Http.Get
            'This is required for our WebDAV server 
            request.SendChunked = True
            request.Headers.Add("Translate", "F")
            Dim response As HttpWebResponse = Nothing
            Try
                'Get the response
                response = DirectCast(request.GetResponse(), HttpWebResponse)
            Catch ex As Exception
                MessageBox.Show("An error occurred while attempting to retrieve data from the remote server.  " & _
                                    "The following error occurred while attempting to retrieve the data:" & vbCrLf & vbCrLf & _
                                    ex.Message)
            End Try
            'Create the buffer for storing the bytes read from the server
            Dim byteTransferRate As Integer = 4096         '4096 bytes = 4 KB
            Dim bytes(byteTransferRate - 1) As Byte
            Dim bytesRead As Integer = 0    'Indicates how many bytes were read 
            Dim totalBytesRead As Long = 0  'Indicates how many total bytes were read
            Dim contentLength As Long = 0   'Indicates the length of the file being downloaded
            
            'This variable will reference the stream from the WebDav server
            Dim s As IO.Stream = Nothing
            'Create a new file to write the downloaded data to
            Dim fs As New IO.FileStream(txtDestination.Text, IO.FileMode.Create, _
                                        IO.FileAccess.Write)
            Try
                'Get the stream from the server
                s = response.GetResponseStream()
                contentLength = CLng(response.GetResponseHeader("Content-Length"))
                pb.Minimum = 0
                pb.Maximum = CInt(contentLength \ byteTransferRate)
                pb.Value = 0
                Do
                    'Read from the stream
                    bytesRead = s.Read(bytes, 0, bytes.Length)
                    If bytesRead > 0 Then
                        totalBytesRead += bytesRead
                        'Write to file
                        fs.Write(bytes, 0, bytesRead)
                        'Update progress
                        pb.Value = CInt(totalBytesRead \ byteTransferRate)
                        If pb.Value Mod 500 = 0 Then _
                            Application.DoEvents()
                    End If
                Loop While bytesRead > 0
            Catch ex As Exception
                MsgBox("The following error occurred while attempting to download the file:  " & vbCrLf & vbCrLf & _
                    ex.Message, MsgBoxStyle.Exclamation, "Error downloading file")
            End Try
            s.Close()
            s.Dispose()
            s = Nothing
            fs.Close()
            fs.Dispose()
            fs = Nothing
            'Validate the downloaded file.  Both must be an exact match 
            '  for the file to be considered a valid download.
            If totalBytesRead <> contentLength Then
                Dim result As DialogResult = _
                    MessageBox.Show("The downloaded file did not download successfully, because the length of the downloaded file " & _
                                    "does not match the length of the file on the remote site.  Would you like to " & _
                                    "delete the downloaded file?", "Download File Validate Failed", _
                                    MessageBoxButtons.YesNo, MessageBoxIcon.Warning)
                If result = Windows.Forms.DialogResult.Yes Then
                    Try
                        'Force the collection of 0 generation so any fs references will be released
                        '  before attempting to delete the file
                        GC.Collect(0)
                        My.Computer.FileSystem.DeleteFile(txtDestination.Text)
                    Catch ex As Exception
                        MessageBox.Show("An error occurred while attempting to delete the following file." & vbCrLf & vbCrLf & txtDestination.Text, _
                                        "Unable to delete file", MessageBoxButtons.OK, MessageBoxIcon.Error)
                    End Try
                End If
            Else 'totalBytesRead = contentLength
                'Files that are smaller than the byteTransferRate will not cause the
                '  progressbar to update
                If pb.Maximum = 0 Then pb.Maximum = 1 : pb.Value = 1
                MessageBox.Show("The file has downloaded successfully!", "Download Complete", _
                                    MessageBoxButtons.OK, MessageBoxIcon.Information)
            End If
            response.Close()
            response = Nothing


    FB

    mardi 30 octobre 2012 13:26
  • le fichier d'origine est de 28129 octet

    Après l'exécution de :

    response = DirectCast(request.GetResponse(), HttpWebResponse)

    response.ContentLength retourne 10183


    FB

    mardi 30 octobre 2012 13:33
  • Bonjour

    Essayez d’enlever

    request.SendChunked = True

    Aussi, enregistrez le fichier téléchargé et vérifiez si c’est bien différente ou  c’est seulement ContentLengthqui n’est pas bon. 
    
    
    Cordialement,  


    Aurel BERA, Microsoft
    Microsoft propose ce service gratuitement, dans le but d'aider les utilisateurs et d'élargir les connaissances générales liées aux produits et technologies Microsoft. Ce contenu est fourni "tel quel" et il n'implique aucune responsabilité de la part de Microsoft.

    mercredi 31 octobre 2012 11:37
    Propriétaire
  • j'avais une erreur dans l'url...

    Ou plutot, je ne prenais pas la bonne url.

    ...


    FB


    mercredi 31 octobre 2012 14:33