Click here to Skip to main content
16,014,748 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hey!

I hope someone of you can help me with this:
I would like to call a API with a WebRequest and send a file (which is stored on the server)

Dim request As HttpWebRequest
Dim response As HttpWebResponse = Nothing
Dim reader As StreamReader
Dim address As Uri
Dim byteData() As Byte
Dim reader0 As StreamReader

Dim gui As String = Guid.NewGuid().ToString()
Dim encoding As String = "iso-8859-1"
Dim head As String = String.Format("--------------------------------", gui)
Dim foot As String = String.Format("--------------------------------", gui)
Dim contents As StringBuilder = New StringBuilder()
contents.AppendLine(head)
contents.AppendLine(String.Format("Content-Disposition: form-data; name=""files[]""; filename=""HELLO.pdf"""))

Dim fInfo As New FileInfo(Server.MapPath("~/HELLO.pdf"))
Dim numBytes As Long = fInfo.Length
Dim fStream As New FileStream(Server.MapPath("~/HELLO.pdf"), FileMode.Open, FileAccess.Read)
Dim br As New BinaryReader(fStream)
Dim data As Byte() = br.ReadBytes(CInt(numBytes))
br.Close()
fStream.Close()
contents.AppendLine(String.Format("Content-Type: application/pdf"))
contents.AppendLine()
contents.AppendLine(data.ToString)
contents.AppendLine()
contents.AppendLine(foot)
contents.AppendLine(String.Format("Content-Disposition: form-data; name=""recipients[]"""))
contents.AppendLine()
contents.AppendLine("name@example.com")
contents.AppendLine()
contents.AppendLine(foot & "--")

address = New Uri("http://api.example.com/signs.json")
request = DirectCast(WebRequest.Create(address), HttpWebRequest)
request.Method = "POST"
request.Headers("Authorization") = "Bearer YzM0OGY4NThlMWM0YjZlNWFmNjkzNzg4Y2ViMTdkZmYxMadfsdafdafmEzYWU5N2VlNWExNA"
request.ContentType = String.Format("multipart/form-data; boundary={0}", gui)
Dim bytes As Byte() = System.Text.Encoding.GetEncoding(encoding).GetBytes(contents.ToString())
request.ContentLength = bytes.Length
request.Host = "api.example.com"

Try
Dim NewStream As Stream = request.GetRequestStream
NewStream.Write(bytes, 0, bytes.Length)
' Get response
response = request.GetResponse()


' Get the response stream into a reader
reader0 = New StreamReader(response.GetResponseStream())
ViewBag.test = reader0.ReadToEnd()
reader0.Close()
Catch ex As WebException
ViewBag.test = ex.StackTrace
End Try

When I am using this code nothing received at the server

What I am doing wrong?
Thanks a lot!!!!
Cheers
Posted
Comments
j snooze 19-May-14 17:09pm    
Try looking at this c# example and convert it to vb.net.
http://www.codeproject.com/Tips/443588/Simple-Csharp-FTP-Class.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900