RSS

Navigation



Quick Search'''



Visual Basic .NET - HTTP FORM POST

RSS
The following code demonstrates how to use HTTPWebRequest and HTTPWebResponse from the .NET Framework to perform an HTTP FORM POST:
 


Imports PaymentKeys.API_Tookit

’Define a command object for populating command parameters
Dim PK_Command As New PK_PaymentKeyAdmin_Command

’Build the command  to send to the PaymentKeys Application Framework

PK_Command.command = "paymentkey.activate"

PK_Command.version = "1.0"
PK_Command.api_call_id = Guid.NewGuid.ToString

PK_Command.paymentkey = "v1111_00000_00000_00000.pk"

’Serialize the command object to a JSON string
Dim serializer As New System.Web.Script.Serialization.JavaScriptSerializer()
Dim api_call As String = serializer.Serialize(PK_Command)

’Create HMACSHA1 signature hash for the JSON command string using the GatewayCode
Dim sha1 As New System.Security.Cryptography.HMACSHA1()

’Specify the GatewayCode secret key

sha1.Key = System.Text.Encoding.UTF8.GetBytes("PK_Demo")

’Convert the JSON command string to a byte array
Dim byteArray As Byte() = System.Text.Encoding.UTF8.GetBytes(api_call)

’Generate the hash signature
Dim api_sig As String = Convert.ToBase64String(sha1.ComputeHash(byteArray))

'Initialize .Net web request variables
Dim PostURL As String = "https://www.paymentkeys.com/api.rest/appserver"
Dim http_request As HttpWebRequest = CType(WebRequest.Create(PostURL), HttpWebRequest)

'Define the data to post
Dim postData As New StringBuilder

postData.Append("api_KeyID=PaymentKeys_Demo.pk")

postData.Append("&api_sig=" & Server.UrlEncode(api_sig))

postData.Append("&api_call=" & Server.UrlEncode(api_call))

postData.Append("&api_output=json")

’Encode the post data into a byte array
Dim encodedData As New System.Text.UTF8Encoding
Dim postData_byteArray As Byte() = encodedData.GetBytes(postData.ToString)

’Set http request headers

http_request.Method = "POST"

http_request.ContentType = "application/x-www-form-urlencoded"

http_request.ContentLength = postData_byteArray.Length

’Stream the post data to the PaymentKeys application gateway
Dim request_stream As Stream = http_request.GetRequestStream()

request_stream.Write(postData_byteArray, 0, postData_byteArray.Length)

request_stream.Close()

’Process the response
Dim http_response As HttpWebResponse = Nothing
Dim response_stream As StreamReader = Nothing
Dim JSON_Response As String = String.Empty

Try

http_response = CType(http_request.GetResponse(), HttpWebResponse)
response_stream = New StreamReader(http_response.GetResponseStream(), Encoding.UTF8)
JSON_Response = response_stream.ReadToEnd()
Catch Ex As WebException
’Handle http communication errors here
Finally
If Not http_response Is Nothing Then
http_response.Close()
http_response = Nothing
End If
End Try

’Deserialize JSON response string into response object
If Not JSON_Response = String.Empty Then


Dim PK_Response As PK_PaymentKey_Activate_Response
PK_Response = serializer.Deserialize(Of PK_PaymentKey_Activate_Response)(JSON_Response)

’Parse the Response per your application and policy rules
Select Case PK_Response.status
Case ...

End If
   

   
 

 

ScrewTurn Wiki version 5.2.0.8. Some of the icons created by FamFamFam.