简体   繁体   中英

HttpWebRequest / HttpWebResponse Base 64 problem

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();

您需要先对Base64字符串进行编码(使用Server.Encode),然后再将其与字符串连接。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM