472,796 Members | 1,691 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,796 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 1454

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: erikbower65 | last post by:
Using CodiumAI's pr-agent is simple and powerful. Follow these steps: 1. Install CodiumAI CLI: Ensure Node.js is installed, then run 'npm install -g codiumai' in the terminal. 2. Connect to...
0
linyimin
by: linyimin | last post by:
Spring Startup Analyzer generates an interactive Spring application startup report that lets you understand what contributes to the application startup time and helps to optimize it. Support for...
0
by: erikbower65 | last post by:
Here's a concise step-by-step guide for manually installing IntelliJ IDEA: 1. Download: Visit the official JetBrains website and download the IntelliJ IDEA Community or Ultimate edition based on...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Sept 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
14
DJRhino1175
by: DJRhino1175 | last post by:
When I run this code I get an error, its Run-time error# 424 Object required...This is my first attempt at doing something like this. I test the entire code and it worked until I added this - If...
5
by: DJRhino | last post by:
Private Sub CboDrawingID_BeforeUpdate(Cancel As Integer) If = 310029923 Or 310030138 Or 310030152 Or 310030346 Or 310030348 Or _ 310030356 Or 310030359 Or 310030362 Or...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
0
by: Mushico | last post by:
How to calculate date of retirement from date of birth
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...

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.