使用 .NET 通过 HTTP 发送 Base64 编码字符串时出现问题
好吧,问题是我正在尝试通过 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);
Ok, so the problem is I'm trying to send a byte array via HTTP encoded as base64. While the string I receive on the other end is the same size as the original string, the strings themselves are not identical, hence I can't decode the string back to the original byte array.
Also, I've done conversion to/from base64 on the client side before sending the string and everything works fine. It's after it gets sent that the problem occurs.
Is there anything I'm missing? Any particular formatting type? I've tried using EscapeData() but the string is too large.
Thank you in advance
edit: code
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);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
下面第二行就是问题所在。
<代码>
如果您查看 Base 64 索引表,最后两个字符(+ 和 /)不是 URL安全的。因此,当您将其附加到请求时,您必须对图像进行 URL 编码。
我不是 .net 人员,但第二行应该写成类似
<代码>
服务器端无需进行任何更改。只要找出对一段字符串进行 URL Encode 的系统调用是什么即可。
The second line below is the problem.
If you look at Base 64 index table, the last two characters (+ and /) are NOT URL safe. So, when you append it to request, you MUST URL Encode the image.
I am not a .net guy, but the second line should be written something like
No changes needed on the server side. Just find out what the system call is to URL Encode a piece of string.
我会建议尝试两件事
在内容类型中包含字符集,因为您依赖 UTF8 -
rq.ContentType = "application/x-www-form-urlencoded; charset=utf-8"
使用 StreamWriter 写入请求流,就像使用 StreamReader 读取它一样。
I will suggest two things to try out
Include charset in content type as your are relying on UTF8 -
rq.ContentType = "application/x-www-form-urlencoded; charset=utf-8"
Use StreamWriter to write to request stream as you are using StreamReader to read it.