Advertisement

06.01.2008 at 11:49AM PDT, ID: 23448352
[x]
Attachment Details

Retrieve values from WebResponse

Asked by jayv21 in Microsoft Visual Basic.Net, Programming for ASP.NET

Tags: VB.NET

Hi All,
I am doing a webrequest\webresponse to a third party payment gateway. I get the response back fine, but it comes back in the format somewhat similar to a querystring for example this is what the response data would look like (response=1&responsetext=SUCCESS&authcode=123456&transactionid=2
81719471&avsresponse=&cvvresponse=M&orderid=&type=sale&response_code=100).  My problem is I don't know how to get just the values of the response back so I can assign each value to a variable and pass it into an SQL statement to record the responses.  I've included a small snippet of code so you have and idea of what I'm doing. Any suggestions? Thanks.  Start Free Trial
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
Private Sub CreditCardPayment()
        Dim result As WebResponse
        Try
            Dim req As WebRequest
            Dim reqStream, recStream As Stream
            Dim tStamp As Long
            Dim encode As Encoding
            Dim sr As StreamReader
            Dim num As String
            Dim exp As String
            Dim url As String
            Dim responseText As String
            url = "https://secure.appsgateway.com/api/transact.php" 'TESTURL
            num = "4111111111111111"
            exp = "0711"
            req = WebRequest.Create(url)
            req.Method = "POST"
            req.ContentType = "application/x-www-form-urlencoded"
            Dim SomeBytes() As Byte
            Dim UrlEncoded As New StringBuilder
            Dim reserved() As Char = {ChrW(63), ChrW(61), ChrW(38)}
            Dim data As String 'post data
            data = "username=demo" & _
                 "&password=password" & _
                 "&amount=10.00" & _
                 "&payment=creditcard" & _
                 "&type=sale" & _
                 "&orderdescription=" & _
                 "&ccnumber=" & num & _
                 "&ccexp=" & exp & _
                 "&address1=Test Lane" & _
                 "&zip=84065" & _
                 "&firstname=some" & _
                 "&lastname=guy" & _
                 "&email=j@hotmail.com" & _
                 "&orderid=5555" & _
                 "&cvv=999"
 
 
            If data <> Nothing Then
                Dim i As Integer = 0
                Dim j As Integer
                While i < data.Length
                    j = data.IndexOfAny(reserved, i)
                    If j = -1 Then
                        UrlEncoded.Append(HttpUtility.UrlEncode(data.Substring(i, data.Length - i)))
                        Exit While
                    End If
                    UrlEncoded.Append(HttpUtility.UrlEncode(data.Substring(i, j - i)))
                    UrlEncoded.Append(data.Substring(j, 1))
                    i = j + 1
                End While
                SomeBytes = System.Text.Encoding.UTF8.GetBytes(UrlEncoded.ToString())
                req.ContentLength = SomeBytes.Length
                reqStream = req.GetRequestStream()
                reqStream.Write(SomeBytes, 0, SomeBytes.Length)
                reqStream.Close()
            Else
                req.ContentLength = 0
            End If
 
            result = req.GetResponse()
            recStream = result.GetResponseStream()
            encode = System.Text.Encoding.GetEncoding("utf-8")
            sr = New StreamReader(recStream, encode)
            Dim read(256) As Char
            Dim count As Integer = sr.Read(read, 0, 256)
            Dim theString As New StringBuilder
            Do While count > 0
                Dim str As String = New String(read, 0, count)
                theString.Append(str)
                count = sr.Read(read, 0, 256)
            Loop
            Dim responses() As String = theString.ToString
 
            \\ TO D0: Get values from web response and assign them to            variables
[+][-]06.01.2008 at 12:11PM PDT, ID: 21688098

Assisted solutions are selected by the member who asked the question as a comment that contributed to their question's solution.

Start your 7-day free trial to view this Assisted Solution or ask the Experts your question.

 
[+][-]06.01.2008 at 12:34PM PDT, ID: 21688182

View this solution now by starting your 7-day free trial. Setting up your free trial is quick, easy, and secure. We will return you to this solution, unlocked, when you're done.

 

About this solution

Zones: Microsoft Visual Basic.Net, Programming for ASP.NET
Tags: VB.NET
Sign Up Now!
Solution Provided By: MrClyfar
Participating Experts: 2
Solution Grade: A
 
 
[+][-]06.01.2008 at 12:39PM PDT, ID: 21688195

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
 
Loading Advertisement...
20080716-EE-VQP-32 / EE_QW_2_20070628