HttpWebRequest / HttpWebResponse Base 64 problem

佐手、 提交于 2020-01-06 08:26:55

问题


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

回答1:


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



来源:https://stackoverflow.com/questions/1265612/httpwebrequest-httpwebresponse-base-64-problem

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!