使用 .NET 通过 HTTP 发送 Base64 编码字符串时出现问题

发布于 2024-09-15 10:04:27 字数 1129 浏览 8 评论 0原文

好吧,问题是我正在尝试通过 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

z祗昰~ 2024-09-22 10:04:27

下面第二行就是问题所在。

<代码>

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

如果您查看 Base 64 索引表,最后两个字符(+ 和 /)不是 URL安全的。因此,当您将其附加到请求时,您必须对图像进行 URL 编码。

我不是 .net 人员,但第二行应该写成类似
<代码>

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

服务器端无需进行任何更改。只要找出对一段字符串进行 URL Encode 的系统调用是什么即可。

The second line below is the problem.

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

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

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

No changes needed on the server side. Just find out what the system call is to URL Encode a piece of string.

长亭外,古道边 2024-09-22 10:04:27

我会建议尝试两件事

  1. 在内容类型中包含字符集,因为您依赖 UTF8 -

    rq.ContentType = "application/x-www-form-urlencoded; charset=utf-8"

  2. 使用 StreamWriter 写入请求流,就像使用 StreamReader 读取它一样。

I will suggest two things to try out

  1. Include charset in content type as your are relying on UTF8 -

    rq.ContentType = "application/x-www-form-urlencoded; charset=utf-8"

  2. Use StreamWriter to write to request stream as you are using StreamReader to read it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文