Wednesday, August 1, 2007

Upload File w/ VB.NET using HTTP Post

I don't want to give away the family receipt, but using VB.NET to upload a file using HTTP POST to CURL was a miserable experience. I found that most of the documentation was for the open source community and that most in the .NET community have evolved into a more elegant way off passing data to the rest of the world...However, when required to interface with the rest of the world this may be a handy little code block.

Therefore, in its entirity I present this lovely block of happiness (without warranty - of course).


Public Shared Function UploadFileEx(ByVal url As String, ByVal username As String, ByVal password As String, ByVal uploadfile As String) As String
Dim fileFormName As String = "FILE1"
Dim contenttype As String = "application/octet-stream"
Dim postdata As String = ""
Dim uri As Uri = New Uri(url)
Dim boundary As String = "----------" + DateTime.Now.Ticks.ToString("x")
Dim webreq As HttpWebRequest = CType(WebRequest.Create(uri), HttpWebRequest)
Dim myCred As NetworkCredential = New NetworkCredential(username, password)
Dim MyCrendentialCache As New CredentialCache
MyCrendentialCache.Add(uri, "Basic", myCred)
webreq.Credentials = MyCrendentialCache
webreq.Headers.Add("Cookie", "SMCHALLENGE=YES")
webreq.ContentType = "multipart/form-data; boundary=" & boundary
webreq.Method = "POST"
webreq.KeepAlive = False
webreq.Headers.Add(HttpRequestHeader.Authorization, "Basic RjAwMEZUOkYwMDBGVA==") '64 bit encoding...
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' ------------8c9a237fc120593
'Content-Disposition: form-data; name="FILE1"; filename="F000FT_08012007_input.csv"
'Content-Type: application/octet-stream
'
'
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Dim ph As String = ""
ph += "--"
ph += boundary
ph += Chr(13) & Chr(10) & ""
ph += "Content-Disposition: form-data; name="""
ph += fileFormName
ph += """; filename="""
ph += Path.GetFileName(uploadfile)
ph += """"
ph += Chr(13) & Chr(10) & ""
ph += "Content-Type: "
ph += contenttype
ph += Chr(13) & Chr(10) & ""
ph += Chr(13) & Chr(10) & ""
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'------------8c9a23ab9505a79
'
'
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Dim pf As String = "--" + boundary + Chr(13) & Chr(10) & Chr(13) & Chr(10) & ""
Dim postHeaderBytes As Byte() = Encoding.UTF8.GetBytes(ph)
Dim FooterBytes As Byte() = Encoding.ASCII.GetBytes(pf)
Dim fileStream As FileStream = New FileStream(uploadfile, FileMode.Open, FileAccess.Read)
Dim length As Long = postHeaderBytes.Length + fileStream.Length + FooterBytes.Length()
webreq.ContentLength = length
Dim requestStream As Stream = webreq.GetRequestStream
requestStream.Write(postHeaderBytes, 0, postHeaderBytes.Length)
Dim b1 As Integer = CType(Math.Max(4096, CType(fileStream.Length, Integer)), System.UInt32)
Dim buffer(1024) As Byte
Dim bytesRead As Integer = -1
Do While Not (bytesRead = 0)
bytesRead = fileStream.Read(buffer, 0, buffer.Length)
requestStream.Write(buffer, 0, bytesRead)
Loop
requestStream.Write(FooterBytes, 0, FooterBytes.Length)
requestStream.Close()
Dim response As HttpWebResponse = Nothing
Try
response = webreq.GetResponse()
Catch ex As Exception
MsgBox(ex.Message)
End Try
Dim s As Stream = response.GetResponseStream
Dim sr As StreamReader = New StreamReader(s)
Dim rv As String = sr.ReadToEnd.ToString()
sr.Close()
s.Close()
Return rv
End Function