Results 1 to 2 of 2

Thread: Sending XML via POST

  1. #1

    Thread Starter
    New Member
    Join Date
    Feb 2012
    Posts
    1

    Sending XML via POST

    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%>&nbsp;</td><td><h2>Rcopia Engine API Test Form</h2></td></tr>
    <tr><td width=10%>&nbsp;</td><td><h3>Command: send_patient</h3></td></tr>
    <tr><td width=10%>&nbsp;</td><td><h3>Send To: &lt;EngineUploadURL&gt;</h3></td></tr>
    
    <form action="https://example.com/servlet/example.servlet.EngineServlet" method=POST>
    
    <tr>
    <td width=10%>&nbsp;</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%>&nbsp;</td><td>&nbsp;</td></tr>
    <tr><td width=10%>&nbsp;</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?

  2. #2
    PowerPoster
    Join Date
    Feb 2006
    Posts
    24,482

    Re: Sending XML via POST

    This is a VB.Net coding question, not a deployment question.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width