Public Function UploadFileBinary(ByVal localFile As String, ByVal uploadUrl As String)
Try
' Define the fact we don't want fully trusted certs
ServicePointManager.CertificatePolicy = New MyCertificateValidation
' We want ssl 3
'ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3
' Create a request
Dim req As HttpWebRequest = WebRequest.Create(uploadUrl)
' Create a new CredentialCache object and fill it with the network
' credentials required to access the server.
Dim credentials As NetworkCredential = New NetworkCredential("username", "password")
Dim credentialCache As System.Net.CredentialCache = New CredentialCache
credentialCache.Add(New System.Uri(uploadUrl), "Basic", credentials)
req.Method = "PUT"
req.Credentials = credentialCache
'req.AuthenticationLevel = AuthenticationLevel.None
req.AllowWriteStreamBuffering = True
' Retrieve request stream
Dim reqStream As Stream = req.GetRequestStream()
' Open the local file
Dim rdr As FileStream = New FileStream(localFile, FileMode.Open)
' Allocate byte buffer to hold file contents
Dim inData() As Byte = New Byte(4096) {}
' loop through the local file reading each data block
' and writing to the request stream buffer
Dim bytesRead As Integer = rdr.Read(inData, 0, inData.Length)
While (bytesRead > 0)
reqStream.Write(inData, 0, bytesRead)
bytesRead = rdr.Read(inData, 0, inData.Length)
End While
rdr.Close()
reqStream.Close()
'req.AuthenticationLevel = AuthenticationLevel.None
req.GetResponse()
Return True
Catch webEx As WebException
If (webEx.Status = WebExceptionStatus.ProtocolError) Then
Debug.WriteLine(TAB & "Web Exception caught in Upload.UploadFileBinary() - Authorisation Failed" & NL & TAB & webEx.Message.ToString)
Else
Debug.WriteLine(TAB & "Web Exception caught in Upload.UploadFileBinary()." & NL & TAB & webEx.Message.ToString)
End If
Return False
Catch ex As Exception
Debug.WriteLine(TAB & "Exception caught in Upload.UploadFileBinary()." & NL & TAB & ex.Message.ToString)
Return False
End Try
End Function