Link to home
Start Free TrialLog in
Avatar of prowebinteractiveinc
prowebinteractiveinc

asked on

Post XML string in vb.net

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.
        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.
        Console.WriteLine(responseFromServer)
        ' Clean up the streams.
        reader.Close()
        dataStream.Close()
        response.Close()
    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.... :(
SOLUTION
Avatar of Robert Schutt
Robert Schutt
Flag of Netherlands image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of prowebinteractiveinc
prowebinteractiveinc

ASKER

I am getting a few errors while doing what you say, can you guide me alittle more please. I dont know if im understanding correctly, I understand what to put in my button but where do I put
    Public Sub New()
        'What do I put here ?
    End Sub 

Open in new window


also is that namespace that I marked out needed ?
Please explain

Thanks
Well, if there's not a specific need to change this class to be able to instantiate it, I would leave it like it is, so don't add Sub New() for now. The namespace is optional.

Just the call of the function should be something like:
    Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
        WebRequestPostExample.Main()
    End Sub

Open in new window

This can be done because it's a Shared Sub, you don't need to instantiate an object for that. So delete your old line of code:
Dim process As New WebRequestPostExample

Open in new window

ok, but there is no way to know if its doing anything ? it compiled well before, but Im not sure what its doing...
yes sure, best way IMHO is to put a breakpoint on the line in your button_click and then use F11 (debug - step into) to follow the code into the external file, it should execute fine.
In the function "Console.WriteLine()" is used to debug what is going on, here is a capture of the Output window on my system (first OK, then an xml repsonse document):
User generated image
can I not put a messagebox at the end of the class to know if the file has been executed
Well, ultimately I suspect you will want to process and/or return the data that was received in the string 'responseFromServer' instead of writing it to the console.
I am not getting what your getting in your output - debug ?
right, ultimately, I would like to take the string, and save it either in a text file, or save it on mySQL server, then parse the info and take what I need which is most probably going to be if its a approved and the transaction number
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
> parse the info and take what I need

Right, so maybe it would be an idea to return this text into an xml document so that you can get the fields you need from that?
Im kinda convinced that the class is not even being executed for some reason, how can I display the string responsefromserver ?, I put in a messagebox, but I never got the messagebox...
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
set a breakpoint where the code shows that it should output xml
the break points confirmed that the class is not being executed !
so, take a step back; put a break point in the button click and press F11 until you reach the call into the other class, what happens? It should step into the sub, if not something else is wrong.
in my class which sends the xmlI have

Public Class WebRequestPostExample

    Public Shared Sub Main()
           'sending xml

Open in new window


in my button all I have is
        Try
            Dim process As New WebRequestPostExample
        Catch myerror As Exception
            MessageBox.Show(myerror.Message)
        End Try
        'Label1.Text = "Button works"

Open in new window


so Im calling WebRequestPostExample but what about sub Main() ?
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Bingo !
Thanks Expert !
Heh heh, no problem man, let me know if you need more help, I'll check back in an hour or so.