[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1180
  • Last Modified:

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.... :(
0
prowebinteractiveinc
Asked:
prowebinteractiveinc
  • 11
  • 11
4 Solutions
 
Robert SchuttSoftware EngineerCommented:
It looks like you need to call this from your button_click code with:
WebRequestPostExample.Main()

Open in new window

because it's a shared sub. A class that is instantiated with New needs a constructor like this:
    Public Sub New()
        ' put class initialisation here
    End Sub

Open in new window

0
 
prowebinteractiveincAuthor Commented:
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
0
 
Robert SchuttSoftware EngineerCommented:
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

0
Get quick recovery of individual SharePoint items

Free tool – Veeam Explorer for Microsoft SharePoint, enables fast, easy restores of SharePoint sites, documents, libraries and lists — all with no agents to manage and no additional licenses to buy.

 
prowebinteractiveincAuthor Commented:
ok, but there is no way to know if its doing anything ? it compiled well before, but Im not sure what its doing...
0
 
Robert SchuttSoftware EngineerCommented:
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.
0
 
Robert SchuttSoftware EngineerCommented:
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):
capture of output
0
 
prowebinteractiveincAuthor Commented:
can I not put a messagebox at the end of the class to know if the file has been executed
0
 
Robert SchuttSoftware EngineerCommented:
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.
0
 
prowebinteractiveincAuthor Commented:
I am not getting what your getting in your output - debug ?
0
 
prowebinteractiveincAuthor Commented:
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
0
 
Robert SchuttSoftware EngineerCommented:
You should be able to find that output (if you're running in Debug mode), by choosing the menu Debug -> Windows -> Output.
capture2
0
 
Robert SchuttSoftware EngineerCommented:
> 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?
0
 
prowebinteractiveincAuthor Commented:
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...
0
 
Robert SchuttSoftware EngineerCommented:
Have you tried debug-stepping through it and/or setting breakpoints?
0
 
prowebinteractiveincAuthor Commented:
set a breakpoint where the code shows that it should output xml
0
 
prowebinteractiveincAuthor Commented:
the break points confirmed that the class is not being executed !
0
 
Robert SchuttSoftware EngineerCommented:
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.
0
 
prowebinteractiveincAuthor Commented:
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() ?
0
 
Robert SchuttSoftware EngineerCommented:
You still have
Dim process As New WebRequestPostExample

Open in new window

That should be
WebRequestPostExample.Main()

Open in new window

0
 
prowebinteractiveincAuthor Commented:
Bingo !
0
 
prowebinteractiveincAuthor Commented:
Thanks Expert !
0
 
Robert SchuttSoftware EngineerCommented:
Heh heh, no problem man, let me know if you need more help, I'll check back in an hour or so.
0

Featured Post

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

  • 11
  • 11
Tackle projects and never again get stuck behind a technical roadblock.
Join Now