i convert an image to base64 string to upload through HttpWebRequest in c#.on server side when i receive base64 string the “+” signs have been converted to white spaces” “. it give me error to convert this base64 string to byte array.i do not want to make any change on server side(in web services). i want to resolve this issues on client side.my client side code is as follow.
//////////////////
WSManagerResult wsResult = new WSManagerResult();
try
{
HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create(serviceURL);
req.Method = "POST";
req.ProtocolVersion = HttpVersion.Version11;
req.ContentType = "application/x-www-form-urlencoded";
// req.ContentType = "application/x-www-form-urlencoded; charset=utf-8";
// req.CookieContainer = new CookieContainer();
string content = string.Empty;
foreach (KeyValuePair<string, string> entry in paramDic)
{
// entry.Value is a base 64 string gene rated from image
content = content + entry.Key + "=" + entry.Value + "&&";
}
content = content.TrimEnd('&'); // input parameter if u have more that one //a=b&dd=aa
req.ContentLength = content.Length;
// req = URLEncode(content);
Stream wri = req.GetRequestStream();
byte[] array = Encoding.ASCII.GetBytes(content);
if (array.Length > 0)
wri.Write(array, 0, array.Length);
wri.Flush();
wri.Close();
WebResponse rsp = (HttpWebResponse)req.GetResponse();
byte[] b = null;
using (Stream stream = rsp.GetResponseStream())
using (MemoryStream ms = new MemoryStream())
{
int count = 0;
do
{
byte[] buf = new byte[1024];
count = stream.Read(buf, 0, 1024);
ms.Write(buf, 0, count);
} while (stream.CanRead && count > 0);
b = ms.ToArray();
}
wsResult.result = Encoding.ASCII.GetString(b);
}
catch (Exception e)
{
clsException.ExceptionInstance.HandleException(e);
wsResult.error = e.Message;
}
return wsResult;
All the “+” signs in above base64 string are converted in to ” ” white spaces.which causes the issue as describe above.
Please help me to resolve this issue.
Regards
shah Khalid
There exists an encoding called Base64Url encode that’s designed for just that. However you may have to do the encoding / decoding on their respective ends.
In Base64 Url, it converts
+to-and/to_so that it can be safely passed over the wire without the standard URL encoders adding weirdness like spaces, or percent-hex