Receive posted Base64 String from WCF RRS feed

  • Question

  • please help i am new for WCF,

    my work : Somebody is sending the image converted in Base64 String format,

    and i have to convet the base64 String into image & save that image.

    I have referred many things but not able to get it.

    i am getting error Like

    HTTP Error

    from IIS 7.5

    please anybody help me..

    i am completely struct on this
    Tuesday, January 21, 2014 7:29 AM


  • Hi,

    If you're sending the base64-encoded data of the image, you need to receive it as a JSON string, since JSON arrays (even byte arrays) are represented as JSON arrays. Another option would be to send the image itself, without any encoding, and receive it in raw mode. The code below shows all those options. And you can find more information about the raw mode at

    public class Image
    { [ServiceContract] public interface ITest { [OperationContract] [WebInvoke(RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)] string PostViaBase64(string base64); [OperationContract] [WebInvoke(RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)] string PostViaBinary(Stream data); [OperationContract] [WebInvoke(RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)] string PostViaByteArray(byte[] imgBytes); } public class Service : ITest { public string PostViaBase64(string base64) { byte[] imgBytes = Convert.FromBase64String(base64); return String.Format("First few bytes: {0:X2} {1:X2} {2:X2}", imgBytes[0], imgBytes[1], imgBytes[2]); } public string PostViaBinary(Stream data) { byte[] imgBytes = new byte[3]; data.Read(imgBytes, 0, imgBytes.Length); return String.Format("First few bytes: {0:X2} {1:X2} {2:X2}", imgBytes[0], imgBytes[1], imgBytes[2]); } public string PostViaByteArray(byte[] imgBytes) { return String.Format("First few bytes: {0:X2} {1:X2} {2:X2}", imgBytes[0], imgBytes[1], imgBytes[2]); } } static void SendRequest(string uri, string contentType, byte[] body) { HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create(uri); req.Method = "POST"; req.ContentType = contentType; req.GetRequestStream().Write(body, 0, body.Length); req.GetRequestStream().Close(); HttpWebResponse resp; try { resp = (HttpWebResponse)req.GetResponse(); } catch (WebException e) { resp = (HttpWebResponse)e.Response; } Console.WriteLine("HTTP/{0} {1} {2}", resp.ProtocolVersion, (int)resp.StatusCode, resp.StatusDescription); foreach (string headerName in resp.Headers.AllKeys) { Console.WriteLine("{0}: {1}", headerName, resp.Headers[headerName]); } Console.WriteLine(); Stream respStream = resp.GetResponseStream(); if (respStream != null) { string responseBody = new StreamReader(respStream).ReadToEnd(); Console.WriteLine(responseBody); } else { Console.WriteLine("HttpWebResponse.GetResponseStream returned null"); } Console.WriteLine(); } static byte[] GetJsonArray(byte[] data) { StringBuilder sb = new StringBuilder(); sb.Append('['); for (int i = 0; i < data.Length; i++) { if (i > 0) sb.Append(','); sb.Append((int)data[i]); } sb.Append(']'); return Encoding.UTF8.GetBytes(sb.ToString()); } public static void Test() { string baseAddress = "http://" + Environment.MachineName + ":8000/Service"; ServiceHost host = new ServiceHost(typeof(Service), new Uri(baseAddress)); host.AddServiceEndpoint(typeof(ITest), new WebHttpBinding(), "").Behaviors.Add(new WebHttpBehavior()); host.Open(); Console.WriteLine("Host opened"); byte[] image = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13 }; // some image array string imageBase64 = Convert.ToBase64String(image); string jsonInput = "\"" + imageBase64 + "\""; byte[] jsonInputBytes = Encoding.UTF8.GetBytes(jsonInput); SendRequest(baseAddress + "/PostViaBase64", "application/json", jsonInputBytes); SendRequest(baseAddress + "/PostViaBinary", "image/jpeg", image); SendRequest(baseAddress + "/PostViaByteArray", "application/json", GetJsonArray(image)); Console.Write("Press ENTER to close the host"); Console.ReadLine(); host.Close(); } }

    If you meet any error, it will be better for you to post the code and error information here.

    Best Regards,
    Amy Peng


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Wednesday, January 22, 2014 6:53 AM