Here's some code I use to post a credit card transaction, which returns to
the caller. You can then redirect to thankyou:
Private Function viaKlixPostTransaction(ByVal trans As
viaKlixTransaction) As String
modErr.EnterFunction("OrderForm.aspx.vb.viaKlixPostTransaction")
Dim sbldr As New StringBuilder
Dim params As NameValueCollection = trans.GetValues
Dim iEnum As IEnumerator = params.Keys.GetEnumerator
' Build the request string from the parameters
Do While iEnum.MoveNext
Dim sKey As String = iEnum.Current
If Not params.Item(sKey) Is Nothing AndAlso
params.Item(sKey).ToString <> "" Then
sbldr.Append(sKey)
sbldr.Append("=")
sbldr.Append(Server.UrlEncode(params.Item(sKey)))
sbldr.Append("&")
End If
Loop
sbldr.Remove(sbldr.Length - 1, 1) 'Remove trailing "&"
'Post the request
Dim dataResponse As String
Dim dataBody As String = sbldr.ToString
Dim webResponse As HttpWebResponse
Dim webRequestStream As System.IO.Stream
Dim webRequest As HttpWebRequest
Dim responseStream As Stream
' Create the RequestStream
Try
webRequest =
webRequest.Create("
https://www2.viaklix.com/process.asp")
' Note - "KeepAlive = False" seems to be needed to avoid errors,
apparently caused by the client
' (this application) thinking the connection is being
maintained while the server or
' firewall may disconnect it. Apparently, if the client
doesn't maintain it, it will know
' enough to re-establish it when it needs it.
webRequest.KeepAlive = False
webRequest.Method = "POST"
webRequest.ContentType = "application/x-www-form-urlencoded"
' Create request body
webRequest.ContentLength = dataBody.Length
webRequestStream = webRequest.GetRequestStream()
Catch ex As Exception
modErr.WriteLog("Creating RequestStream: " & ex.ToString, 0)
End Try
' Write the WebRequest
Try
Dim writer As New StreamWriter(webRequestStream)
writer.Write(dataBody)
'TODO - writer.close can probably go into a Finally block...
Try 'Getting an exception on this shouldn't stop us...
writer.Close() 'Closes the StreamWriter and the underlying
stream
Catch ex As Exception
modErr.WriteLog("Writer.Close: " & ex.ToString, 0)
End Try
Catch ex As Exception ' Catches error on writer.write
modErr.WriteLog(ex.ToString, 0)
modErr.WriteLog(dataBody, 1) 'Looking for data dependency
modErr.ExitFunction()
Return Nothing
End Try
' Get the response from viaKlix
Try
webResponse = webRequest.GetResponse()
responseStream = webResponse.GetResponseStream()
Dim readStream As New System.IO.StreamReader(responseStream)
dataResponse = readStream.ReadToEnd
readStream.Close() 'Closes the StreamReader and the underlying
stream
modErr.ExitFunction()
Return dataResponse
Catch ex As Exception
modErr.WriteLog("Reading Response: " & ex.ToString, 0)
modErr.ExitFunction()
Return Nothing
End Try
End Function