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



我的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 ,设置内容长度并将您的数据直接复制到请求流中?只要你在另一端正确解释它,你就会没事的。

如果使用

WCF 数据服务,则可以通过单独的二进制流发送二进制数据

[数据可以发送]作为单独的二进制资源流。这是访问和更改二进制大型对象 (BLOB) 数据的推荐方法,这些数据可能表示照片、视频或任何其他类型的二进制编码数据。

http://msdn.microsoft.com/en-us/library/ee473426.aspx

您还可以使用 HttpWebRequest 上传二进制数据

通过 HttpWebRequest 传递二进制数据

使用 HTTPWebrequest 上传文件(多部分/表单数据)

最新更新