c# - 将 utf8 转换为 1251

标签 c# .net

好的,问题如下。我必须从在线表格中获取信息,然后再次提交(已更改)。该页面位于“windows-1251”中,根据我的要求,我得到了它:

{
         // used to build entire input
        StringBuilder sb = new StringBuilder();

        // used on each read operation
        byte[] buf = new byte[8192];

        // prepare the web page we will be asking for
        HttpWebRequest request = (HttpWebRequest)
            WebRequest.Create(url);

        // execute the request
        HttpWebResponse response = (HttpWebResponse)
            request.GetResponse();

        // we will read data via the response stream
        Stream resStream = response.GetResponseStream();

        string tempString = null;
        int count = 0;

        do
        {
            // fill the buffer with data
            count = resStream.Read(buf, 0, buf.Length);

            // make sure we read some data
            if (count != 0)
            {
                // translate from bytes to ASCII text
                tempString = Encoding.GetEncoding(1251).GetString(buf, 0, count);

                // continue building the string
                sb.Append(tempString);
            }
        }
        while (count > 0); // any more data to read?

        // print out page source
        // Console.WriteLine(sb.ToString());
        return sb.ToString();

所以在那之后,我剥离了页面并将我的数据放入一个数组中,我需要再次将它发布到网站上,但它需要被编码。我使用 System.Net.WebUtility.UrlEncode 进行编码,但我的脚本和 firefox 得到的结果完全不同(我使用篡改数据获得了原始结果)。因此,我们将不胜感激任何帮助或想法。顺便说一句,在我获得编码数据(来自脚本)后,我在 http://www.url-encode-decode.com/ 在线尝试了它并且只有 UTF-8 返回正确的结果。所以我猜我必须将它转换为 1251 并且我尝试这样做但它仍然是乱码并且与它应该是的完全不同......所以如果你能帮助我那就太好了......: )

编辑:

Expected: %E3%F0%E0%E4+%D1%EE%F4%E8%FF
Actual: %D0%B3%D1%80%D0%B0%D0%B4+%D0%A1%D0%BE%D1%84%D0%B8%D1%8F
f2[11] = град София
-       f2  {string[180]}   string[]
    [0] "127.0.0.1" string
    [1] "1348247257"    string
    [2] "1n132135075318435" string
    [3] "151669n1"  string
    [4] "0" string
    [5] null    string
    [6] null    string
    [7] null    string
    [8] "2" string
    [9] "12871449760521116" string
    [10]    "14"    string
    [11]    "град София"    string
    [12]    "Враждебна" string
    [13]    "42.707984,23.41341,0~бул. Ботевградско шосе"   string
    [14]    "42.7100653,23.4274006,1"   string

最佳答案

答案(通过评论找到)是替换 WebUtility.UrlEncodeHttpUtility.UrlEncode‌​ .此方法有一个将编码作为参数的重载。

关于c# - 将 utf8 转换为 1251,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12557123/

相关文章:

c# - 我应该如何清除通用静态类中的字段?

c# - 如何验证 WPF 中的数据并设置默认值?

c# - 将所选元素的引用从列表添加到列表

c# - 当 ComboBox ItemsSource 更改时,WPF Combobox 更新但不显示显示

c# - 从 Office 本身更新 ClickOnce VSTO 加载项不会更新加载项

c# - 使用 ReaderWriterLockSlim 调试死锁

c# - 使用 OpenXML 和 C# 处理 word 文档

c# - 关于字符串是原始类型的分歧

.net - 反转 WPF 图像的颜色

c# - 如何将样式放在单独的 .xaml 文件中