How to POST base64 to Webservice

Jason picture Jason · Aug 5, 2013 · Viewed 13k times · Source

I want POST one parameter which is base64 to webservice, but it doesn't work.

    string postData = "base64=hySGNg22yX0AIjNQvNJw0xHEbi32NH+iRVKDkbOmUiKpxW0/S6rUgfLmSa527Y8zZ3GmEzfXMB8ry8fH5GjGeJFGqFxDC4A2hLlvIFzyiDeUCqloGDAJoOLa8Jnif1C8nwRte0GNCRM5RXkshLKZP2itZclgDCYz6OgAF7oLNluoB44cZ3AgF1Q8hVgME8LeqgOqxsIzTt3Xn+96j7zp/wA=";
    byte[] dataArray = Encoding.Default.GetBytes(postData);

    HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(ServerPage);
    request.Method = "POST";
    request.ContentLength = dataArray.Length;
    request.ContentType = "application/x-www-form-urlencoded";

    Stream dataStream = null;
    try
    {
        dataStream = request.GetRequestStream();
    }
    catch (Exception)
    {
    }

    dataStream.Write(dataArray, 0, dataArray.Length);
    dataStream.Close();

but parameter in requestHeader changes to:

base64=hySGNg22yX0AIjNQvNJw0xHEbi32NH%2BiRVKDkbOmUiKpxW0%2FS6rUgfLmSa527Y8zZ3GmEzfXMB8ry8fH5GjGeJFGqFxDC4A2hLlvIFzyiDeUCqloGDAJoOLa8Jnif1C8nwRte0GNCRM5RXkshLKZP2itZclgDCYz6OgAF7oLNluoB44cZ3AgF1Q8hVgME8LeqgOqxsIzTt3Xn%2B96j7zp%2FwA%3D

so WebService receives a wrong parameter:

[WebMethod]
public string GenerateOrder(string base64)
{ 
    byte[] buffer = Convert.FromBase64String(base64); //Exception here
} 

Answer

Kai Hartmann picture Kai Hartmann · Aug 5, 2013

Use HttpUtility.UrlDecode(base64), before converting it back from base64.