HttpWebrequest and uploading an image as multipart

Jun 15 2009 6:15 AM
Hi I have got a standard Httpwebrequest working and its all fine, here is what i have so far: lRep = "http://192.192.192.1:8080/Test_0_11/Tasks?param1=json WebReq = WebReq.Create(lREp) WebReq.Timeout = 600000 WebReq.AllowWriteStreamBuffering = True WebReq.Method = "GET" WebResp = WebReq.GetResponse() WebStream = WebResp.GetResponseStream StreamR = New StreamReader(WebStream) XCCC = StreamR.ReadToEnd StreamR.Close() WebStream.Close() WebResp.Close() WebReq.Abort() Problem is that I want to upload an image through this httpwebrequest, i am told that when calling AddImage request inorder to send an image to the server, the request should be Multipart/form-data, what does that mean?. The name of the parameter is called PhotoTK, so i presume the url may look something like this: lRep = "http://192.192.192.1:8080/Test_0_11/AddImage?PhotoTK= but what do i put into ? How do i get an image and put it into a string? The closest thing i have found is the following, but i am not sure how to convet it for my needs. Dim filepath As String = "\Temp\1.jpg" 'IO.Path.Combine(Application.StartupPath, "filename.ext") Dim url As String = "http://www.FAKESITE.com/php/Upload/index.php" Dim boundary As String = "qwerty.flp" 'IO.Path.GetRandomFileName Dim header As New System.Text.StringBuilder() header.AppendLine("--" & boundary) header.Append("Content-Disposition: form-data; name=""uploaded_file"";") header.AppendFormat("filename=""{0}""", IO.Path.GetFileName(filepath)) 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) 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 + endboundarybytes.Length req.Method = "POST" Dim s As IO.Stream = req.GetRequestStream s.Write(headerbytes, 0, headerbytes.Length) Dim filebytes() As Byte = ReadAllBytes(filepath) s.Write(filebytes, 0, filebytes.Length) s.Write(endboundarybytes, 0, endboundarybytes.Length) s.Close() ANy help would be appreciated