I have been struggling for 4 days to write a connection to an XML REST API. I can't figure out what is wrong with my code that it won't connect propertly. The goal is to make a web service that connects directly to the REST API. When I code it as a front end doing a form post it works fine, but I can't code the VB on the backend to do the same thing.
Any help would be much appreciated. Also, how do you convert the contents of a filestream to a string. I was able to convert a file to byte array, but not to a string of those bytes.
Protected Sub PostMessage()
Try
Dim apiaddy As String = "https://apps.destination.com/messages.xml"
Dim Boundary As String = "-F"
Dim request As HttpWebRequest = CType(WebRequest.Create(apiaddy), HttpWebRequest)
' Set the Method property of the request to POST.
request.Method = "POST"
'request.Accept = "*/*"
request.UserAgent = "curl/7.16.3"
request.ProtocolVersion = HttpVersion.Version11
request.Referer = "http://api.myhost.com/"
request.SendChunked = True
'System.Net.ServicePointManager.Expect100Continue = False
' Create POST data and convert it to a byte array.
' Partner Webservice Info
Dim apikey As String = "dalfkjasdlkjsadflkjasdfljkasdf"
Dim did As String = "asdlkfjsld"
Dim externalid As String = "54654654"
Dim messagesender As String = "545-555-6666"
Dim requestinghost As String = "api.myhost.com"
Dim testFileName As String = "F:\WebSites\testwavs\testwav.wav"
Dim fileContents As String = ""
'Load Wav File Data
Dim file1 As New FileStream(testFileName, FileMode.Open, FileAccess.Read)
Dim filedata(file1.Length) As Byte
file1.Read(filedata, 0, file1.Length)
fileContents = file1.ToString()
Dim postData As String = ""
postData += Boundary + Chr(13) + Chr(10) + "account_did=" + HttpUtility.UrlEncode(did) + Chr(13) + Chr(10)
postData += Boundary + Chr(13) + Chr(10) + "api_key=" + HttpUtility.UrlEncode(apikey) + Chr(13) + Chr(10)
postData += Boundary + Chr(13) + Chr(10) + "Content-Disposition: form-data; name=""message[external_id]""" + Chr(13) + Chr(10) + Chr(13) + Chr(10)
postData += HttpUtility.UrlEncode(externalid) + Chr(13) + Chr(10)
postData += Boundary + Chr(13) + Chr(10) + "Content-Disposition: form-data; name=""message[sender]""" + Chr(13) + Chr(10) + Chr(13) + Chr(10)
postData += HttpUtility.UrlEncode(messagesender) + Chr(13) + Chr(10)
postData += Boundary + Chr(13) + Chr(10) + "Content-Disposition: form-data; name=""message[requesting_host]""" + Chr(13) + Chr(10) + Chr(13) + Chr(10)
postData += HttpUtility.UrlEncode(requestinghost) + Chr(13) + Chr(10)
postData += Boundary + Chr(13) + Chr(10) + "Content-Disposition: form-data; name=""file""; filename=""testwav.wav""" + Chr(13) + Chr(10)
postData += "Content-Type: audio/x-wav" + Chr(13) + Chr(10) + Chr(13) + Chr(10)
postData += fileContents+ Chr(13) + Chr(10) + Boundary + "--" + Chr(13) + Chr(10)
Dim encoding As New System.Text.ASCIIEncoding()
Dim byteArray As Byte() = encoding.GetBytes(postData)
' Set the ContentType property of the WebRequest.
request.ContentType = "multipart/form-data; boundary=" + Boundary
' Set the ContentLength property of the WebRequest.
request.ContentLength = byteArray.Length
request.Headers("ContentLenght") = byteArray.Length
Dim postStream As Stream = Nothing
' Write data
Try
postStream = request.GetRequestStream()
postStream.Write(byteArray, 0, byteArray.Length)
Finally
If Not postStream Is Nothing Then postStream.Close()
End Try
Dim reader As StreamReader
Dim response As HttpWebResponse = Nothing
Try
' Get response
response = DirectCast(request.GetResponse(), HttpWebResponse)
' Get the response stream into a reader
reader = New StreamReader(response.GetResponseStream())
' Console application output
lbl.Text = reader.ReadToEnd()
Finally
If Not response Is Nothing Then response.Close()
End Try
Catch Ex As Exception
MsgBox(Ex.Message, MsgBoxStyle.Exclamation, "Error Encountered")
End Try
End Sub