c# - 在没有 base64 编码的情况下通过 https 发送字节数组的方法?

标签 c# encoding httphandler

我的 outlook 插件通过 https 发送大文件。目前,我在客户端使用 Convert.ToBase64String(),在 IIS 端的 http 处理程序上使用 Convert.FromBase64String()。

这已经出现了一些性能问题,而且我也在通过 SSL 保护数据,所以我真的想问是否有任何方法可以通过 https 转换字节数组而不使用会降低接收方 cpu 性能的编码结束。

我的客户代码:

string requestURL = "http://192.168.1.46/websvc/transfer.trn";
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(requestURL);
request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";

// Chunk(buffer) is converted to Base64 string that will be convert to Bytes on  the handler.
string requestParameters = @"fileName=" + fileName + @"&secretKey=testKey=" + @"&currentChunk=" + i + @"&totalChunks=" + totalChunks + @"&smGuid=" + smGuid +
                            "&data=" + HttpUtility.UrlEncode(Convert.ToBase64String(bytes));

// finally whole request will be converted to bytes that will be transferred to HttpHandler
byte[] byteData = Encoding.UTF8.GetBytes(requestParameters);

request.ContentLength = byteData.Length;

Stream writer = request.GetRequestStream();
writer.Write(byteData, 0, byteData.Length);
writer.Close();
// here we will receive the response from HttpHandler
StreamReader stIn = new StreamReader(request.GetResponse().GetResponseStream());
string strResponse = stIn.ReadToEnd();
stIn.Close();

存在性能问题的服务器代码:

byte[] buffer = Convert.FromBase64String(context.Request.Form["data"]); // 

最佳答案

您不必使用 contentType application/x-www-form-urlencoded 发送。为什么不设置为类似 application/octet-stream 的内容,设置内容长度并将数据直接复制到请求流中?只要您在另一端正确解释它,您就会没事。

关于c# - 在没有 base64 编码的情况下通过 https 发送字节数组的方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11336561/

相关文章:

c# - 在 Razor View 引擎中将 HTML 元素作为 Function<> 传递

sql - 如何修复 PostgreSQL 中的双重编码?

asp.net - 有什么方法可以在.NET 中以编程方式添加 HttpHandler?

c# - ParseExact 无法解析 RFC 3339 Internet 日期/时间格式的字符串

c# - 为什么 .NET 中的已检查算法有时比未检查的算法快?

c# - 如何模拟 Entity Framework 导航属性智能?

algorithm - 基本 X 字符串编码

c# - 将 Word 保存为 UTF-8 编码的 HTML

asp.net - .ashx 处理程序中的 View 状态?

iis-7 - 无法使用 IIS 7 设置 WebDAV