Am uploading multiple files from a client app in another language. I wish to manually write the headers which are sent to my php script but have hit a brick wall. Bare in mind this is a VB.NET 2010 client contacting the php script.
Dim boundary As String = IO.Path.GetRandomFileName
Dim boundary2 As String = IO.Path.GetRandomFileName
Dim header As New System.Text.StringBuilder()
header.AppendLine("--" & boundary)
header.AppendLine("Content-Disposition: form-data; name=""uploaded_file[]"";")
header.AppendLine("Content-type: multipart/mixed, boundary=" & boundary2)
header.AppendLine("--" & boundary2)
header.AppendFormat("Content-disposition: form-data; filename=""{0}""", IO.Path.GetFileName(filepath))
header.AppendLine("Content-type: file")
header.AppendLine()
Dim header2 As New System.Text.StringBuilder()
header2.AppendLine("--" & boundary2)
header2.AppendFormat("Content-disposition: form-data; filename=""{0}""", IO.Path.GetFileName(filepath2))
header2.AppendLine("Content-type: file")
header2.AppendLine()
Dim headerbytes() As Byte = System.Text.Encoding.UTF8.GetBytes(header.ToString)
Dim header2bytes() As Byte = System.Text.Encoding.UTF8.GetBytes(header2.ToString)
Dim endboundary2bytes() As Byte = System.Text.Encoding.ASCII.GetBytes(vbNewLine & "--" & boundary2 & "--")
Dim endboundarybytes() As Byte = System.Text.Encoding.ASCII.GetBytes(vbNewLine & "--" & boundary & "--" & vbNewLine)
Dim req As Net.HttpWebRequest = Net.HttpWebRequest.Create(url)
req.ContentType = "multipart/form-data; boundary=" & boundary
req.ContentLength = headerbytes.Length + New IO.FileInfo(filepath).Length + header2bytes.Length + New IO.FileInfo(filepath2).Length + endboundary2bytes.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(filepath)
s.Write(filebytes, 0, filebytes.Length)
s.Write(header2bytes, 0, header2bytes.Length)
Dim file2bytes() As Byte = My.Computer.FileSystem.ReadAllBytes(filepath2)
s.Write(file2bytes, 0, file2bytes.Length)
s.Write(endboundary2bytes, 0, endboundary2bytes.Length)
s.Write(endboundarybytes, 0, endboundarybytes.Length)
s.Close()
Dim response As Net.WebResponse = req.GetResponse()
s = response.GetResponseStream()
Dim reader As New IO.StreamReader(s)
Dim responseFromServer As String = reader.ReadToEnd()
reader.Close()
s.Close()
response.Close()