txdev
txdev

Reputation: 267

Receive byte array over HTTP in an ASP.NET 2.0 app

Trying to transfer a byte array to an ASP.NET 2.0 app in a light-weight manner without using SOAP. I decided to use a generic HTTP handler (.ashx) that interprets the HTTP request body as a Base64 string, decoding it to a byte array and saving to disk.

<%@ WebHandler Language="C#" Class="PdfPrintService" %>

using System;
using System.Web;

public class PdfPrintService : IHttpHandler {

    public void ProcessRequest (HttpContext context) {
        string body;
        using (System.IO.StreamReader reader = 
            new System.IO.StreamReader(context.Request.InputStream))
        {
            body = reader.ReadToEnd();
        }

        byte[] bytes = System.Convert.FromBase64String(body);
        String filePath = System.IO.Path.GetTempFileName() + ".pdf";
        System.IO.File.WriteAllBytes(filePath, bytes);

        // Print file.
        XyzCompany.Printing.PrintUtility.PrintFile(filePath);
    }

    public bool IsReusable {
        get {
            return false;
        }
    }
}

The client application (an iOS app in my case) will simply have to encode the bytes as Base64 and post them to the URL of this generic handler (ashx).

I imagine there is a better, more orthodox way to do this. Any ideas are appreciated!

Upvotes: 0

Views: 613

Answers (1)

RandomUs1r
RandomUs1r

Reputation: 4190

The thing that comes to mind is POST & GET Requests handled through classes like the HttpWebResponse class. http://msdn.microsoft.com/en-us/library/system.net.httpwebresponse%28v=vs.71%29.aspxx You can have your iOS app try to POST to the ASP.NET app, which is then set up to receive the POST and parse it for your byte array, which you'd include. More, or less, this is how some data was sent across the internet before SOAP.. all SOAP is is a schema for these types of requests.

Upvotes: 1

Related Questions