C# – HttpWebRequest / HttpWebResponse Base 64 problem

asp.netcpayment-gateway

I'm trying to post to a url, in order to generate a page.
The url is specified elsewhere in my application, and originates from a bank.

The parameters i need to specify are:
Pareq – this is a long string, specified elsewhere in my application
TermUrl – a url the bank uses to post back to (my application)
MD – some random string to identify the order.

The relevant parameter here is the pareq –

I have the below code on the page, and Response.Write(response) at the end, to create a page from the request.
However, i am getting an error returned from the posted to url-
PaReq message not based64 encoded.

From my code, you can see i've tried to base 64 encode it, but somewhere i'm going wrong….

                HttpWebRequest req = (HttpWebRequest)WebRequest.Create(acsUrl);

            byte[] toEncodeAsBytes  = System.Text.ASCIIEncoding.ASCII.GetBytes(pareq);

            string data = String.Format("PaReq={0}&TermUrl={1}&MD={2}", System.Convert.ToBase64String(toEncodeAsBytes), "www.return.com", "wsdfskdjglke");
            byte[] buffer = Encoding.UTF8.GetBytes(data);

            req.Method = "POST";
            req.ContentType = "application/x-www-form-urlencoded";
            req.ContentLength = buffer.Length;
            req.CookieContainer = new CookieContainer(); // enable cookies

            Stream reqst = req.GetRequestStream(); // add form data to request stream
            reqst.Write(buffer, 0, buffer.Length);
            reqst.Flush();
            reqst.Close();

            HttpWebResponse res = (HttpWebResponse)req.GetResponse();

            Stream resst = res.GetResponseStream();
            StreamReader sr = new StreamReader(resst);
            string response = sr.ReadToEnd();

Best Answer

You need to encode (using Server.Encode) your Base64 string before you concat it with your string.

Related Topic