Skip to content

Instantly share code, notes, and snippets.

@remcotolsma
Created June 7, 2017 07:50
Show Gist options
  • Save remcotolsma/f8ef80bdee6b8c5fac1c5a274dfa54cb to your computer and use it in GitHub Desktop.
Save remcotolsma/f8ef80bdee6b8c5fac1c5a274dfa54cb to your computer and use it in GitHub Desktop.
Post XML message to lookup server.
Sub Upload_File()
Dim fName As String = "C:\Temp\Bestand1.xml"
Dim wUser As String = "User"
Dim wPassword As String = "Password"
Dim url As New Uri("https://www.website.nl/upl.php")
Dim cred As New CredentialCache()
cred.Add(url, "Basic", New NetworkCredential(wUser, wPassword))
Dim boundary As String = Path.GetFileName(fName)
Dim header As New System.Text.StringBuilder()
' Onderstaaande is misschien niet meer nodig?
header.AppendLine("--" & boundary)
' Onderstaaande is misschien niet meer nodig?
header.Append("Content-Disposition: form-data; name=""file"";")
' Onderstaaande is misschien niet meer nodig?
header.AppendFormat("filename=""{0}""", IO.Path.GetFileName(fName))
header.AppendLine()
' Onderstaaande is misschien niet meer nodig?
header.AppendLine("Content-Type: application/octet-stream")
header.AppendLine()
Dim headerbytes() As Byte = System.Text.Encoding.UTF8.GetBytes(header.ToString)
Dim endboundarybytes() As Byte = System.Text.Encoding.ASCII.GetBytes(vbNewLine & "--" & boundary & "--" & vbNewLine)
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12 '<<------------------------------------------ eventueel uitschakelen
Dim req As Net.HttpWebRequest = Net.HttpWebRequest.Create(url)
req.Credentials = cred '<<------------------------------------------ eventueel uitschakelen
' Onderstaaande is misschien niet meer nodig?
req.ContentType = "multipart/form-data; boundary=" & boundary
req.ContentLength = headerbytes.Length + New IO.FileInfo(fName).Length + endboundarybytes.Length
req.Method = "POST"
Dim s As IO.Stream = req.GetRequestStream
s.Write(headerbytes, 0, headerbytes.Length)
Dim filebytes() As Byte = My.Computer.FileSystem.ReadAllBytes(fName)
s.Write(filebytes, 0, filebytes.Length)
s.Write(endboundarybytes, 0, endboundarybytes.Length)
s.Close()
Dim result As String
Using response As WebResponse = req.GetResponse()
Using reader As New StreamReader(response.GetResponseStream())
result = reader.ReadToEnd()
End Using
End Using
Console.WriteLine(result)
End Sub
Sub Upload_File()
Dim fName As String = "C:\Temp\Bestand1.xml"
Dim wUser As String = "User"
Dim wPassword As String = "Password"
Dim url As New Uri("https://www.website.nl/upl.php")
Dim cred As New CredentialCache()
cred.Add(url, "Basic", New NetworkCredential(wUser, wPassword))
Dim boundary As String = Path.GetFileName(fName)
Dim header As New System.Text.StringBuilder()
header.AppendLine("--" & boundary)
header.Append("Content-Disposition: form-data; name=""file"";")
header.AppendFormat("filename=""{0}""", IO.Path.GetFileName(fName))
header.AppendLine()
header.AppendLine("Content-Type: application/octet-stream")
header.AppendLine()
Dim headerbytes() As Byte = System.Text.Encoding.UTF8.GetBytes(header.ToString)
Dim endboundarybytes() As Byte = System.Text.Encoding.ASCII.GetBytes(vbNewLine & "--" & boundary & "--" & vbNewLine)
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12 '<<------------------------------------------ eventueel uitschakelen
Dim req As Net.HttpWebRequest = Net.HttpWebRequest.Create(url)
req.Credentials = cred '<<------------------------------------------ eventueel uitschakelen
req.ContentType = "multipart/form-data; boundary=" & boundary
req.ContentLength = headerbytes.Length + New IO.FileInfo(fName).Length + endboundarybytes.Length
req.Method = "POST"
Dim s As IO.Stream = req.GetRequestStream
s.Write(headerbytes, 0, headerbytes.Length)
Dim filebytes() As Byte = My.Computer.FileSystem.ReadAllBytes(fName)
s.Write(filebytes, 0, filebytes.Length)
s.Write(endboundarybytes, 0, endboundarybytes.Length)
s.Close()
Dim result As String
Using response As WebResponse = req.GetResponse()
Using reader As New StreamReader(response.GetResponseStream())
result = reader.ReadToEnd()
End Using
End Using
Console.WriteLine(result)
End Sub
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment