在 C# 中使用 HttpWebRequest 发送 base64 字符串,将“+”转换为空格问题

本文关键字:转换 问题 空格 字符串 HttpWebRequest base64 发送 | 更新日期: 2023-09-27 18:30:17

当我收到base64字符串时,我将图像转换为base64字符串以通过c#.on服务器端的HttpWebRequest上传,"+"符号已转换为空格"。将此 base64 字符串转换为字节数组会给我错误。我不想在服务器端(在 Web 服务中)进行任何更改。我想在客户端 side.my 客户端代码上解决此问题,如下所示。

///

/

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)
            {

//进入。值是从图像中评级的碱基 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 字符串中的所有"+"符号都转换为"空格。这会导致上述问题。

请帮助我解决此问题。

问候

沙阿·哈立德

在 C# 中使用 HttpWebRequest 发送 base64 字符串,将“+”转换为空格问题

非常感谢 Rob.it 通过在客户端将"+"替换为十六进制"%2B"来解决我的问题,以在 C# 中通过下面的 wire.as 发布数据。

/*Using standard Base64 in URL requires encoding of '+', '/' and '=' characters into special percent-encoded hexadecimal sequences ('+' = '%2B', '/' = '%2F' and '=' = '%3D')*/ 
String stbase64datatopost =stbase64datatopost.Replace("+",@"%2B"); 
stbase64datatopost = stbase64datatopost .Replace("/",@"%2F");      
stbase64datatopost=stbase64datatopost.Replace("=",@"%3D");

存在一种名为 Base64Url 编码的编码,它就是为此而设计的。 但是,您可能必须在各自的末端进行编码/解码。

在 Base64 URL 中,它将+转换为-/转换为_,以便可以安全地通过线路传递,而无需标准 URL 编码器添加空格或十六进制百分比等怪异内容

尝试使用 HttpUtility.UrlEncode(entry) 将 base64 编码数据发布到 Web 服务

Web 服务应该能够在不更改代码的情况下解析它