1

My outlook plugin sends large files over https. Currently I'm using Convert.ToBase64String() on the client side and Convert.FromBase64String() on the http handler on the IIS Side.

This has come to some performance issues, and also i'm also securing the data over SSL so i'm really asking if there's any way to convert a byte array over https without using encoding that will cost cpu performance on the recipient end.

My Client code:

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();

Server code where I have performance issues:

byte[] buffer = Convert.FromBase64String(context.Request.Form["data"]); // 
Ry-
  • 218,210
  • 55
  • 464
  • 476
Dan C.
  • 559
  • 3
  • 8
  • 26

2 Answers2

4

You don't have to send using contentType application/x-www-form-urlencoded. Why not just set as something like application/octet-stream, set a content length and copy your data right into the request stream? As long as you interpret it correctly at the other end, you'll be fine.

spender
  • 117,338
  • 33
  • 229
  • 351
  • Do you have any examples you can share? I guess im just confused because once i take away the base64 conversion errors start popping up that i cant send it like that. – Dan C. Jul 05 '12 at 00:54
0

If you use WCF Data Services, you can send binary data via a separate binary stream

[Data can be sent] as a separate binary resource stream. This is the recommended method for accessing and changing binary large object (BLOB) data that may represent a photo, video, or any other type of binary encoded data.

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

You can also upload binary data using HttpWebRequest

Passing binary data through HttpWebRequest

Upload files with HTTPWebrequest (multipart/form-data)

Community
  • 1
  • 1
Eric J.
  • 147,927
  • 63
  • 340
  • 553
  • Originally, this was a WCF web service, but the amount of padding was causing much more overhead than it was worth. I should mention this isnt for a simple picture being sent but VERY large files (>1GB). I'd really like to not completely change the frint end and back end again, maybe just a more efficient way to use the code in place. – Dan C. Jul 05 '12 at 00:20
  • Added an option to use HttpWebRequest to my answer. – Eric J. Jul 05 '12 at 00:21
  • @gui_s3: Oopps accidentally got some indentation that turned it into a code block :-) Edited. – Eric J. Jul 05 '12 at 00:22
  • Arent I already using HTTPWebRequest? Do you know how would i remove base64 from my code and server side since i'm already sort of doing it this way? – Dan C. Jul 05 '12 at 00:27
  • I think @spender hit it on the head... change the content type and write directly to the stream. – Eric J. Jul 05 '12 at 00:36