我将图像转换为base64字符串以通过c#中的HttpWebRequest上传。当我收到base64字符串时,“+”符号已转换为空格“”。将此base64字符串转换为字节数组时出错。我不想在服务器端(在Web服务中)进行任何更改。我想在客户端解决这个问题。我的客户端代码如下。
/////////////////
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 是一个基于图像的 64 位字符串基因
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;
上面 base64 字符串中的所有“+”符号都转换为“”空格。这会导致上述问题。
请帮我解决这个问题。
问候
沙阿哈立德