简体   繁体   English

HttpWebRequest / HttpWebResponse Base 64问题

[英]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. 我需要指定的参数是:Pareq-这是一个长字符串,在我的应用程序中的其他位置指定TermUrl-银行用来发回(我的应用程序)MD的URL-一些随机字符串来标识顺序。

The relevant parameter here is the pareq - 这里的相关参数是pareq-

I have the below code on the page, and Response.Write(response) at the end, to create a page from the request. 我在页面上具有以下代码,并在最后具有Response.Write(response),以根据请求创建页面。 However, i am getting an error returned from the posted to url- PaReq message not based64 encoded. 但是,我收到从发布到url-PaReq消息的返回错误,该消息不是基于base64编码的。

From my code, you can see i've tried to base 64 encode it, but somewhere i'm going wrong.... 从我的代码中,您可以看到我已尝试对它进行64位编码,但是在某些地方我出了错....

                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),然后再将其与字符串连接。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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