gpt4 book ai didi

.net - 构建发布时 MonoTouch 中的 WebClient 编码问题

转载 作者:可可西里 更新时间:2023-11-01 17:10:39 24 4
gpt4 key购买 nike

我一直遇到 WebClient 行为不同的问题在真实设备上发布构建,而不是在 iOS 模拟器的 Debug模式下构建。

显然,来自服务器的响应得到了不同的解释,并在发布版本中中断: Screenshot from the DEBUG build running in the iOS Simulator Screenshot from the RELEASE build running on an iPhone 4S

This question似乎对类似问题有答案,但我不想在应用程序中对编码进行硬编码,即使它在其生命周期内可能不会改变,这样做似乎是错误的,因为信息已经包含在 HTTP 响应

我已经用 Xamarin 提出了一个问题来进一步调查这个问题。

最佳答案

这里有两个因素在起作用。第一:

it seems wrong to do so as the information is already contained in the HTTP response

是的,它确实看起来不对,但 Microsoft .NET 的 WebClient.Encoding默认情况下与 System.Text.Encoding.Default 相同.

MSDN 引用:

The default value of this property is the encoding returned by Default.

WebClient 的 Mono(和 MonoTouch)实现的行为相同。这经常被忽视(大部分时间都有效),但它是难以找到(不是 MonoTouch,而是 .NET 特定)错误的来源,因为不保证Default值即可。

MSDN 引用:

Different computers can use different encodings as the default, and the default encoding can even change on a single computer.

iOS 模拟器的第二个因素是:模拟器 不是仿真器。这有很多好处(例如,它比 Android 模拟器快得多),但它也有缺点(IMO 很少,但它只会让它们更难被发现)。

这意味着模拟器在使用通用 API 时不会尝试(太多)隐藏底层操作系统(即 OSX),例如获取默认代码页。由于它返回不同的值,System.Text.Encoding.Default 将使用不同的代码页进行初始化,从而导致使用不同的实现。

因此,将您自己的编码设置为 WebClient.Encoding 是解决问题(对于任何 .NET 应用程序)的正确(且安全)方法。

关于.net - 构建发布时 MonoTouch 中的 WebClient 编码问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9656881/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com