使用.NET通过HTTP发送base64编码的字符串

mtr*_*nda 2 .net base64 http transmission

好吧,问题是我试图通过HTTP编码为base64发送一个字节数组.虽然我在另一端接收的字符串与原始字符串的大小相同,但字符串本身并不相同,因此我无法将字符串解码回原始字节数组.

另外,在发送字符串之前,我已经完成了对客户端base64的转换,一切正常.它发送后发生问题.

有什么我想念的吗?任何特定的格式类型?我尝试过使用EscapeData(),但字符串太大了.

先感谢您

编辑:代码

System.Net.WebRequest rq = System.Net.WebRequest.Create("http://localhost:53399/TestSite/Default.aspx");
rq.Method = "POST";
rq.ContentType = "application/x-www-form-urlencoded";
string request = string.Empty;
string image =             Convert.ToBase64String(System.IO.File.ReadAllBytes("c:\\temp.png"));            
request += "image=" + image;
int length = image.Length;
byte[] array = new UTF8Encoding().GetBytes(request);
rq.ContentLength = request.Length;
System.IO.Stream str = rq.GetRequestStream();                        
str.Write(array, 0, array.Length);            
System.Net.WebResponse rs = rq.GetResponse();
System.IO.StreamReader reader = new System.IO.StreamReader(rs.GetResponseStream());
string response = reader.ReadToEnd();
reader.Close();
str.Close();            
System.IO.File.WriteAllText("c:\\temp\\response.txt", response);
Run Code Online (Sandbox Code Playgroud)

Sri*_*nan 5

下面的第二行是问题所在.

string image = Convert.ToBase64String(System.IO.File.ReadAllBytes("c:\temp.png"));
request += "image=" + image;

string image = Convert.ToBase64String(System.IO.File.ReadAllBytes("c:\temp.png"));
request += "image=" + URLEncode(image);

服务器端无需更改.只需找出系统调用的URL URL编码一段字符串.