We help IT Professionals succeed at work.
Get Started

Post XML string in vb.net

Last Modified: 2012-08-13
I found a class online and thought I would give it a try, im kinda new to VB.NET so I can read the code but im not exactly sure whats going on or if it even works

if what I have is wrong Im looking to Post an XML string to a credit card processor, and also receive  a response from the server in XML and parse it - Im hoping someone can help me

note that I cancelled out the namespace... do I need it ? I cant call the class with that namespace there. The code I use to call the class is below  the class

This is what I found
Imports System
Imports System.IO
Imports System.Net
Imports System.Text
'Namespace Examples.System.Net

Public Class WebRequestPostExample

    Public Shared Sub Main()
        Dim xml As String
        xml = "xxxRequestMode=X&xxxRequestData="
        xml += "<?xml version='1.0' encoding='UTF-8'?>"
        xml += "<TranxRequest>"
        xml += "<MerchantNumber>1234567890</MerchantNumber>"
        xml += "<Products>59.95::1::100::Test Product 1::{TEST}</Products>"
        xml += "<xxxName>John Smith</xxxName>"
        xml += "<xxxCompany>ABC Corp</xxxCompany>"
        xml += "<xxxAddress>4 Corporate Sq</xxxAddress>"
        xml += "<xxxCity>TestVille</xxxCity>"
        xml += "<xxxState>NY</xxxState>"
        xml += "<xxxZipCode>123456</xxxZipCode>"
        xml += "<xxxCountry>US</xxxCountry>"
        xml += "<xxxPhone>800-555-1234</xxxPhone>"
        xml += "<xxxEmail>merchant@website.com</xxxEmail>"
        xml += "<xxxCard_Number>4505530000000000</xxxCard_Number>"
        xml += "<xxxCCMonth>09</xxxCCMonth>"
        xml += "<xxxCCYear>2009</xxxCCYear>"
        xml += "<CVV2>123</CVV2>"
        xml += "<xxxTransType>00</xxxTransType>"
        xml += "<xxxAuthentication>Y</xxxAuthentication>"
        xml += "</TranxRequest>"

        ' Create a request using a URL that can receive a post. 
        Dim request As WebRequest = WebRequest.Create("https://secure.internetsecure.com/process.cgi")
        ' Set the Method property of the request to POST.
        request.Method = "POST"
        ' Create POST data and convert it to a byte array.
        Dim postData As String = xml
        Dim byteArray As Byte() = Encoding.UTF8.GetBytes(postData)
        ' Set the ContentType property of the WebRequest.
        request.ContentType = "application/x-www-form-urlencoded"
        ' 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.
        ' 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.
        ' Clean up the streams.
    End Sub
End Class
'End Namespace

Open in new window

Button triggering class
Dim process As New WebRequestPostExample

Open in new window

when I press the button nothing happends.... :(
Watch Question
Software Engineer
This problem has been solved!
Unlock 4 Answers and 22 Comments.
See Answers
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE