Thursday, 24 March 2011

How to send POST in background

Sometimes it needs to send POST in the background and get some info. In our case we had to send POST to the Protx (UK payment gateway - now its SagePay). Here is quick C# script how to do this:


private string ChargeProtx(CreditCardInfo objCardInfo)
{
    System.Net.HttpWebRequest request = 
(System.Net.HttpWebRequest) System.Net.HttpWebRequest.Create("https://ukvpstest.protx.com/VSPSimulator/VSPDirectGateway.asp");


    request.Method = "POST";
    
string postData = "VPSProtocol=2.22&" +
"TxType=PAYMENT&" +
"Vendor=vendorname&" +
"VendorTxCode=" + objCardInfo.OrderID + "&" +
"Amount=" + objCardInfo.Amount.ToString().Replace(",",".")+"&"+
"Currency=GBP&" +
"Description=Direct_payment&" +
"CardHolder=" + objCardInfo.FirstName + " " + objCardInfo.LastName + "&" +
"CardNumber=" + objCardInfo.Number + "&";


    byte[] byteArray = System.Text.Encoding.UTF8.GetBytes(postData);
    request.ContentLength = byteArray.Length;
    System.IO.Stream dataStream = request.GetRequestStream();
    dataStream.Write(byteArray, 0, byteArray.Length);
    dataStream.Close();


    System.Net.HttpWebResponse responseX = (System.Net.HttpWebResponse) request.GetResponse();
    dataStream = responseX.GetResponseStream();
    System.IO.StreamReader reader = new System.IO.StreamReader(dataStream);
    string responseFromServer = reader.ReadToEnd();


    reader.Close();
    dataStream.Close();
    responseX.Close();


return responseFromServer;
}

Hope this helps!

No comments:

Post a Comment

Note: only a member of this blog may post a comment.