473,327 Members | 1,919 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,327 software developers and data experts.

Problems Posting to an XML REST API from VB.Net

1
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
Oct 22 '07 #1
0 1497

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: jfixsen | last post by:
Hello! Oracle 9.2.0.4 SunOS pchi-db01 5.8 Generic_108528-19 sun4u sparc SUNW,Ultra-EnterpriseSystem = SunOS Node = pchi-db01 Release = 5.8 KernelID = Generic_108528-19 Machine = sun4u...
2
by: Jacek Pop³awski | last post by:
In the last week I was working to create script which will read command from socket, call it, return result, stdout, stderr and kill it after timeout. After playing with threads, processes, spawns...
4
by: David | last post by:
Hi, I want to work with Access 2002. I will need to be able to distribute my application to users that may still have access 2000 on their computers. I know access 2002 has a function that...
10
by: BBFrost | last post by:
We just recently moved one of our major c# apps from VS Net 2002 to VS Net 2003. At first things were looking ok, now problems are starting to appear. So far ... (1) ...
19
by: Dales | last post by:
I have a custom control that builds what we refer to as "Formlets" around some content in a page. These are basically content "wrapper" sections that are tables that have a colored header and...
4
by: Stimp | last post by:
This is a query to do with classic ASP (written through vbscript). I apologise in advance to posting to an ASP.NET forum, but my newsserver doesn't have any others listed... plus it's a permissions...
3
by: Andreas | last post by:
Hi! I'm currently developing a DLL that makes use of C++ and .net (mixed) using Visual Studio 2003. Now, as I wanted to move to the new Visual Studio 2005, I converted this project into the...
31
by: damacy | last post by:
hi, there. i have a problem writing a program which can obtain ip addresses of machines running in the same local network. say, there are 4 machines present in the network; , , and and if i...
2
by: Liam Gibbs | last post by:
Hello everyone, This will be my third time posting this, but for some reason, my message isn't get through to the newsgroup, even after hours of waiting. So here goes again. I'm having huge...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.