CelerityDev
New member
- Joined
- Feb 13, 2012
- Messages
- 1
- Programming Experience
- 3-5
I am working on a project where I am writing a some app to interface between our application and a provider over the internet. Our application does not directly communicated with the internet so I am having it write the XML data to a file and then my app to read that file and communicate.
I have been given an example of the POST method but it is in html:
Code:
<html> <body> <table> <tr><td width=10%> </td><td><h2>Rcopia Engine API Test Form</h2></td></tr> <tr><td width=10%> </td><td><h3>Command: send_patient</h3></td></tr> <tr><td width=10%> </td><td><h3>Send To: <EngineUploadURL></h3></td></tr> <form action="https://example.com/servlet/example.servlet.EngineServlet" method=POST> <tr> <td width=10%> </td> <td> <textarea name="xml" rows=20 cols=100> <?xml version="1.0" encoding="UTF-8"?> <RCExtRequest version = "2"> <XML>BLAH</XML> </RCExtRequest> </textarea> </td> </tr> <tr><td width=10%> </td><td> </td></tr> <tr><td width=10%> </td><td><input type="submit" value="Submit Request"></td></tr> </table> </form> </body> </html>
This is my Code:
Code:
Imports System Imports System.IO Imports System.Net Public Class Form1 Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load Me.Show() 'Me.ControlBox = False Dim postData As String = "" Dim FILE_NAME As String = "C:\test.txt" If System.IO.File.Exists(FILE_NAME) = True Then Dim objReader As New System.IO.StreamReader(FILE_NAME) postData = objReader.ReadToEnd MsgBox(postdata) objReader.Close() Else MsgBox("File Does Not Exist") End If ' Create a request using a URL that can receive a post. Dim request As WebRequest = WebRequest.Create("https://example/com/servlet/example.servlet.EngineServlet") ' Set the Method property of the request to POST. request.Method = "POST" ' Create POST data and convert it to a byte array. Dim byteArray As Byte() = System.Text.UTF8Encoding.UTF8.GetBytes(postData) ' Set the ContentType property of the WebRequest. request.ContentType = "text/xml" ' Set the ContentLength property of the WebRequest. request.ContentLength = byteArray.Length ' Get the request stream. Dim dataStream As Stream = request.GetRequestStream() ' Write the data to the request stream. dataStream.Write(byteArray, 0, byteArray.Length) ' Close the Stream object. dataStream.Close() ' Get the response. Dim response As WebResponse = request.GetResponse() ' Display the status. Console.WriteLine(CType(response, HttpWebResponse).StatusDescription) ' Get the stream containing content returned by the server. dataStream = response.GetResponseStream() ' Open the stream using a StreamReader for easy access. Dim reader As New StreamReader(dataStream) ' Read the content. Dim responseFromServer As String = reader.ReadToEnd() ' Display the content. MsgBox(responseFromServer) ' Clean up the streams. reader.Close() dataStream.Close() response.Close() End Sub End Class
I had to leave out the actual web address and not include the actual xml data that being send, but that shouldn't cause any problems.
I am getting a response from the server but it is an error response. Does anyone have any suggestions/corrections on how to fix this?
I have been given an example of the POST method but it is in html:
Code:
<html> <body> <table> <tr><td width=10%> </td><td><h2>Rcopia Engine API Test Form</h2></td></tr> <tr><td width=10%> </td><td><h3>Command: send_patient</h3></td></tr> <tr><td width=10%> </td><td><h3>Send To: <EngineUploadURL></h3></td></tr> <form action="https://example.com/servlet/example.servlet.EngineServlet" method=POST> <tr> <td width=10%> </td> <td> <textarea name="xml" rows=20 cols=100> <?xml version="1.0" encoding="UTF-8"?> <RCExtRequest version = "2"> <XML>BLAH</XML> </RCExtRequest> </textarea> </td> </tr> <tr><td width=10%> </td><td> </td></tr> <tr><td width=10%> </td><td><input type="submit" value="Submit Request"></td></tr> </table> </form> </body> </html>
This is my Code:
Code:
Imports System Imports System.IO Imports System.Net Public Class Form1 Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load Me.Show() 'Me.ControlBox = False Dim postData As String = "" Dim FILE_NAME As String = "C:\test.txt" If System.IO.File.Exists(FILE_NAME) = True Then Dim objReader As New System.IO.StreamReader(FILE_NAME) postData = objReader.ReadToEnd MsgBox(postdata) objReader.Close() Else MsgBox("File Does Not Exist") End If ' Create a request using a URL that can receive a post. Dim request As WebRequest = WebRequest.Create("https://example/com/servlet/example.servlet.EngineServlet") ' Set the Method property of the request to POST. request.Method = "POST" ' Create POST data and convert it to a byte array. Dim byteArray As Byte() = System.Text.UTF8Encoding.UTF8.GetBytes(postData) ' Set the ContentType property of the WebRequest. request.ContentType = "text/xml" ' Set the ContentLength property of the WebRequest. request.ContentLength = byteArray.Length ' Get the request stream. Dim dataStream As Stream = request.GetRequestStream() ' Write the data to the request stream. dataStream.Write(byteArray, 0, byteArray.Length) ' Close the Stream object. dataStream.Close() ' Get the response. Dim response As WebResponse = request.GetResponse() ' Display the status. Console.WriteLine(CType(response, HttpWebResponse).StatusDescription) ' Get the stream containing content returned by the server. dataStream = response.GetResponseStream() ' Open the stream using a StreamReader for easy access. Dim reader As New StreamReader(dataStream) ' Read the content. Dim responseFromServer As String = reader.ReadToEnd() ' Display the content. MsgBox(responseFromServer) ' Clean up the streams. reader.Close() dataStream.Close() response.Close() End Sub End Class
I had to leave out the actual web address and not include the actual xml data that being send, but that shouldn't cause any problems.
I am getting a response from the server but it is an error response. Does anyone have any suggestions/corrections on how to fix this?