HttpWebRequest/HttpWebResponse Base 64问题 [英] HttpWebRequest / HttpWebResponse Base 64 problem
问题描述
我正在尝试发布到url,以生成页面.该网址是在我的应用程序中的其他位置指定的,并且源自银行.
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.
我需要指定的参数是:Pareq-这是一个长字符串,在我的应用程序的其他地方指定TermUrl-银行用于发回(我的应用程序)的URLMD-一些随机的字符串来标识顺序.
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-
The relevant parameter here is the pareq -
我在页面上有下面的代码,最后有Response.Write(response),以根据请求创建页面.但是,我收到了从发布到url-的错误消息PaReq消息未基于64位编码.
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.
从我的代码中,您可以看到我已尝试对它进行64位编码,但是在某些地方我出了错....
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).
You need to encode (using Server.Encode) your Base64 string before you concat it with your string.
这篇关于HttpWebRequest/HttpWebResponse Base 64问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!