1

I have a C# .net web application. I am trying to post a binary data from one application to another using this code

    string url = "path to send the data";

    string result=null;
    string postData = "This is a test that posts this string to a Web server.";
    byte[] fileData = Encoding.UTF8.GetBytes (postData);

    // Create a request using a URL that can receive a post. 
    WebRequest request = WebRequest.Create (url);
    // Set the Method property of the request to POST.
    request.Method = "POST";
    // Create POST data and convert it to a byte array.    

    // Set the ContentType property of the WebRequest.
    request.ContentType = "multipart/form-data";
    // Set the ContentLength property of the WebRequest.
    request.ContentLength = fileData.Length;
    // Get the request stream.
    Stream dataStream = request.GetRequestStream ();
    // Write the data to the request stream.
    dataStream.Write (fileData, 0, fileData.Length);
    // Close the Stream object.
    dataStream.Close ();
    // Get the response.
    WebResponse response = request.GetResponse();
    // Display the status.

    result = ((HttpWebResponse)response).StatusDescription;
    // Get the stream containing content returned by the server.
    dataStream = response.GetResponseStream ();
    // Open the stream using a StreamReader for easy access.
    StreamReader reader = new StreamReader (dataStream);
    // Read the content.
    string responseFromServer = reader.ReadToEnd ();
    // Display the content.

    result = result + responseFromServer;
    // Clean up the streams.
    reader.Close ();
    dataStream.Close ();
    response.Close();

By the above code, I am sending byte[] to a second application. How can I retrieve the posted data (in byte[] format) in the second application?

Soner Gönül
  • 97,193
  • 102
  • 206
  • 364
Sudha
  • 2,078
  • 6
  • 28
  • 53

2 Answers2

1

Note: I assume that you are asking about how to retrieve the posted data in second application and also you have access to the code of second application.

Then if it is a webform application then simply on page_load event you can get file name and file itself as:

string strFileName = Request.Files[0].FileName;
HttpPostedFileBase filesToSave = Request.Files[0];

If this is not the requirement, then edit your question and add more details.

Gaurav Pandey
  • 2,798
  • 4
  • 28
  • 41
  • Thank you gaurav. From which name space I can get the StringUtility class? Do I need to add any dll and from where? – Sudha Apr 04 '13 at 11:49
  • sorry my bad.. Please check the updated answer. You can simply get access to the files with indexes. As you are posting 1 file in a request, you can access it with `Request.Files[0]`; – Gaurav Pandey Apr 04 '13 at 12:28
  • When I used this code string strFileName = Request.Files[0].FileName; HttpPostedFile filesToSave = Request.Files[0]; got an error 'Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index' – Sudha Apr 04 '13 at 12:37
  • ohh.. and what happens when you try to get it from InputStream as @dblood suggested? Try that and tell what happens. – Gaurav Pandey Apr 04 '13 at 14:23
  • 1
    I have used this code : HttpContext context = HttpContext.Current; Stream stream = context.Request.InputStream; Then in the stream, I am getting the same lenth byte[] which I sent by the above code. Here it is getting as a Stream obj. How can I convert into byte[]? – Sudha Apr 09 '13 at 04:56
  • [This link](http://stackoverflow.com/questions/1080442/how-to-convert-an-stream-into-a-byte-in-c) can help you for this I think. – Gaurav Pandey Apr 10 '13 at 05:42
  • Sorry.. I couldn't find Buffer.BlockCopy and Buffer.SetByte in that scope. – Sudha Apr 10 '13 at 09:34
  • Ok.. then use this method.. it should definitely work.. `public static byte[] ReadFully(Stream input) { byte[] buffer = new byte[16*1024]; using (MemoryStream ms = new MemoryStream()) { int read; while ((read = input.Read(buffer, 0, buffer.Length)) > 0) { ms.Write(buffer, 0, read); } return ms.ToArray(); } }` – Gaurav Pandey Apr 10 '13 at 11:20
  • You can have a look [here](http://stackoverflow.com/questions/221925/creating-a-byte-array-from-a-stream) for more details :) – Gaurav Pandey Apr 10 '13 at 11:22
  • And actually you can google for BlockCopy and SetByte methods of System.Buffer.. There is a lot of content on google for these. – Gaurav Pandey Apr 10 '13 at 11:56
1

EDIT: Updated answer to include both Request and Server side. Server side converts Base64 string to a byte[].

If you're going to post binary data that was read into a byte[], you'll have to convert it to a Base64 string on request side to post it.

Client/Request Side:

byte[] byteData = ReadSomeData();

string postData = Convert.ToBase64String(byteData);

Then on the server side, use the HttpContext to get the InputStream from the Request property. You can then use a StreamReader and its ReadToEnd() method to read in the data. You then convert the posted Base64 string to a byte[].

Something like this:

string postData = string.Empty;

using (StreamReader reader = new StreamReader(context.Request.InputStream))
{
    postData = inputStreamReader.ReadToEnd();
}

byte[] data = Convert.FromBase64String(postData);
dblood
  • 1,758
  • 17
  • 21
  • What can be the initialLength, offset and count? – Sudha Apr 09 '13 at 04:59
  • In reader.Read(buffer, offset, count) the buffer should be the char[] right?? – Sudha Apr 09 '13 at 05:02
  • @Sudha - I updated the answer to be more specific and that I think more clearly answers your question. – dblood Apr 12 '13 at 13:28
  • @dblood- When I tried this code got error like : 'The input is not a valid Base-64 string as it contains a non-base 64 character, more than two padding characters, or a non-white space character among the padding characters.' in byte[] data = Convert.FromBase64String(postData); line – Sudha Apr 17 '13 at 07:21
  • Did you Base 64 encode the data on the request side before you posted it as I showed in the example? – dblood Apr 17 '13 at 12:38
  • OMG after so many attempts with different ways, finally I find your solution, moreover so simple! Thanks – Jérôme MEVEL Sep 23 '15 at 13:54