将英镑符号从 Delphi 发送到 C# Web 服务

发布于 2024-07-30 18:05:35 字数 791 浏览 4 评论 0原文

我正在从 Delphi 5 向 C# Web 服务发送一个大字符串,但我在使用英镑 (£) 符号时遇到了很多麻烦。 我从 Delphi 端对字符串进行 URLEncode(这似乎将它们转换为“%A3”)。 当它到达 C# Web 服务时,它显示为“�”。 我尝试使用 StreamReader(如下所示)更改 C# 端字符串的编码,但我能做的最好的事情就是更改为问号 (?)。

MemoryStream mr = new MemoryStream(System.Text.Encoding.Default.GetBytes(myString));
StreamReader sr = new StreamReader(mr, System.Text.Encoding.Default);
string s = sr.ReadToEnd();

如何正确解释 £ 符号?

请帮忙!

(请求更多信息)

Web 服务签名是:

[WebMethod]
public string ReadMyString(string PostedString)

Delphi 5 代码使用我们已经成功使用多年的第三方组件/代码,但这是我们第一次尝试直接与 C# 对话。 代码概要如下所示:

 tmp_Str := URLEncode(myBigString);
 tmp_Str := WinInetPostData(myURL, tmp_Str);

在这两行之间,我已确认 £ 符号已正确转换为“%A3”。

I am sending a large string from Delphi 5 to a C# web service, and I'm having lots of trouble with Pound (£) signs. I URLEncode the string from the Delphi side (which seems to convert them to '%A3'). When it reaches the C# web services it appears as '�'. I have tried changing the encoding of the string on the C# side by using a StreamReader (shown below), but the best I can get it to do is to change to to a question mark (?).

MemoryStream mr = new MemoryStream(System.Text.Encoding.Default.GetBytes(myString));
StreamReader sr = new StreamReader(mr, System.Text.Encoding.Default);
string s = sr.ReadToEnd();

How can I get the £ signs to be interpreted correctly?

Please help!

(Further info requested)

The web service signature is:

[WebMethod]
public string ReadMyString(string PostedString)

The Delphi 5 code uses third party components/code that we've been using successfully for years, but this is the first time we've tried talking directly to C#. An outline of the code is shown below:

 tmp_Str := URLEncode(myBigString);
 tmp_Str := WinInetPostData(myURL, tmp_Str);

Between these two lines I have confirmed that the £ signs have been correctly converted to '%A3'.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

寒冷纷飞旳雪 2024-08-06 18:05:35

根据您在您自己的答案中所写的内容< /a>,看起来问题在于客户端如何对字符串进行编码,而不是服务器如何解释它(尽管无论您使用什么编码,服务器都需要配合)。 您显然希望它被编码为 UTF-8(如果您没有指定任何其他内容,这是 StreamReader 的默认值),但如果您使用 NetMasters 库,我不会感到惊讶using 甚至不知道 UTF-8 或任何其他形式的 Unicode。

Delphi 5 可以通过其 WideString 类型很好地处理 Unicode,但它缺乏很多支持实用函数。 如果您想将代码保留在 NetMasters 中,那么最小的改变就是引入支持 Unicode 的库,例如来自免费 JCL。 在那里您可以找到一个 Utf8Encode 函数,它将接收 WideString 并返回一个 AnsiString,然后该函数适合传递给您现有的 URL 编码功能。

更好的办法是完全摆脱 NM 代码。 免费的 Indy 库 具有 UTF-8 编码和 URL 编码以及所有其他 Internet 编码的功能相关任务。

如果您不在客户端使用 Unicode,则没有理由期望“£”被编码为两字节序列 c2 a3。 这是 U+00a3 的 UTF-8 编码形式,即井号字符的代码点。

如果您没有在客户端上使用Unicode,那么您必须找出您正在使用的代码页。 然后,在创建新的 StreamReader 时在服务器上指定该编码。

Based on what you wrote in your own answer, it looks like the problem is in how the client side is encoding the string, not in how the server is interpreting it (although the server needs to cooperate no matter what encoding you use). You're evidently expecting it to be encoded as UTF-8 (that's the default for StreamReader if you don't specify anything else), but I wouldn't be surprised if the NetMasters library you're using doesn't even know about UTF-8 or any other form of Unicode.

Delphi 5 can handle Unicode just fine via its WideString type, but it lacks a lot of support utility functions. If you want to keep your code with NetMasters, then the minimal change for you is to introduce a Unicode-enabled library, such as the JclUnicode unit from the free JCL. There you can find a Utf8Encode function that will receive a WideString and return an AnsiString, which is then suitable for passing to your existing URL-encoding function.

Better would be to get rid of the NM code altogether. The free Indy library has functions for UTF-8-encoding and URL-encoding, as well as all your other Internet-related tasks.

If you're not using Unicode on the client side, then there's no reason to expect "£" to ever be encoded as the two-byte sequence c2 a3. That's the UTF-8-encoded form of U+00a3, the code point for the pound character.

If you're not using Unicode on the client, then you'll have to find out what code page you are using. Then, specify that encoding on the server when you create the new StreamReader.

执笏见 2024-08-06 18:05:35

知道了! Delphi 中的 URLEncode 函数(使用名为 NMURL 的第三方组件)将 £ 编码为“%A3”,而实际上它应该是“%C2%A3”。 我在 Delphi 端进行了手动替换来纠正它,然后在 C# 端根本不需要任何操作。

感谢您的所有建议。 这将教会我相信旧组件!

Got it! The URLEncode function in Delphi (which uses a third-party component called NMURL) is encoding £ as '%A3', when it should in fact be '%C2%A3'. I did a manual replace on the Delphi side to correct it and then it requires no manipulation at all on the C# side.

Thanks for all your suggestions. That'll teach me to put my faith in old components!

花开半夏魅人心 2024-08-06 18:05:35

我建议在 Delphi 端使用 Base64 编码(将单位 EncdDecd 添加到您的 use 子句和方法 EncodeString 中)并在 C# 端对其进行解码。

public static string DecodeString(string base64EncodedString)
  {
     byte[] dataToDecode = Convert.FromBase64String(base64EncodedString);
     string result = Encoding.ASCII.GetString(dataToDecode);
     return result;
  }

祝你好运。

I would recommend using the Base64 encoding on the Delphi side (add the unit EncdDecd to your uses clause and method EncodeString) and decode it on the C# side by..

public static string DecodeString(string base64EncodedString)
  {
     byte[] dataToDecode = Convert.FromBase64String(base64EncodedString);
     string result = Encoding.ASCII.GetString(dataToDecode);
     return result;
  }

Good luck.

已下线请稍等 2024-08-06 18:05:35

据我所知.NET中的字符串是UTF-16,所以你可以尝试在Delphi中使用相应的Widestring < 2009。在 Delphi 2009 中,字符串默认为 UTF-16,但请确保强制执行编码。

As far as I know string in .NET are UTF-16 so you may try to use the corresponding Widestring in Delphi < 2009. In Delphi 2009 strings are UTF-16 by default but make sure you enforce the encoding.

只有影子陪我不离不弃 2024-08-06 18:05:35

来自 System.Text.Encoding.Default 的 .Net 帮助:

“获取系统当前 ANSI 代码页的编码。”

看起来 0xA3 不在该代码页中作为井号。 将其切换为UTF-8,它应该正确解码该特定字符,但我不能说这是否是整体正确的编码(是否是delphi发出的)。

您可以通过更改第一行来切换到 UTF8,如下所示:


MemoryStream mr = new MemoryStream(System.Text.Encoding.UTF8.GetBytes(myString)

From the .Net help for System.Text.Encoding.Default:

"Gets an encoding for the system's current ANSI code page."

Looks like 0xA3 isn't in that code page as a pound sign. Switch it to UTF-8 and it should decode that particular character correctly, but whether that is the correct encoding overall (whether that is what delphi is emitting) I can't say.

You can switch to UTF8 by changing your first line like so:


MemoryStream mr = new MemoryStream(System.Text.Encoding.UTF8.GetBytes(myString)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文