System.NotSupportedException:没有数据可用于编码1252

时间:2018-06-14 13:01:24

标签: c#

我正在与Trust Commerce Tutorial合作,了解如何生成支付令牌,以便客​​户使用TC受托人主机付款表单。我得到了一个关于如何通过开发团队检索此令牌的示例。

using System;
using System.Net;
using System.IO;
using System.Text;
using System.Collections;
using System.Web;

/** @class TCToken
 * An example class for generating a TrustCommerce Trustee Token
 */
public class TCToken
{

    public static void Main(string [] args)
    {
        string custid = "123456";
        string password = "XXXXXX";
        try {
            // Adapted from http://www.west-wind.com/presentations/dotnetWebRequest/dotnetWebRequest.htm
            string gateway_post_address = "https://vault.trustcommerce.com/trustee/token.php";
            HttpWebRequest req = (HttpWebRequest) WebRequest.Create(gateway_post_address);

            // A sixty second timeout.
            req.Timeout = 60000;

            string post_data = "custid=" + HttpUtility.UrlEncode(custid) +
                                "&password=" + HttpUtility.UrlEncode(password);

            req.Method = "POST";
            byte [] buf = System.Text.Encoding.GetEncoding(1252).GetBytes(post_data);
            req.ContentLength = buf.Length;
            req.ContentType = "application/x-www-form-urlencoded";

            Stream s = req.GetRequestStream();
            s.Write(buf, 0, buf.Length);
            s.Close();

            HttpWebResponse rep = (HttpWebResponse) req.GetResponse();
            Encoding enc = System.Text.Encoding.GetEncoding(1252);
            StreamReader rs = new StreamReader(rep.GetResponseStream(), enc);

            string token = rs.ReadToEnd();

            Console.WriteLine(token);

            rep.Close();
            rs.Close();
        } catch (Exception e) {
            Console.WriteLine(e);
        }
    }
}

我在visual studio中创建了一个新的控制台应用程序,复制了这段代码,并用正确的凭据替换了用户名和密码。当我尝试运行它时,我在控制台中收到以下错误。

  

System.NotSupportedException:没有数据可用于编码1252。   有关定义自定义编码的信息,请参阅文档   用于Encoding.RegisterProvider方法。在   System.Text.Encoding.GetEncoding(Int32 codepage)at   TCToken.Program.Main(String [] args)in   C:\ Users \ xxxx \ source \ repos \ TCToken \ TCToken \ Program.cs:第29行

我试图谷歌这个错误,大多数回复都略高于我的理解。我当然不是C#专家。

4 个答案:

答案 0 :(得分:8)

ckuri说了什么。为了清楚起见,在打开流之前,需要以下代码行(步骤2,3):

System.Text.Encoding.RegisterProvider(System.Text.CodePagesEncodingProvider.Instance);

答案 1 :(得分:3)

.NET Core仅支持ASCII,ISO-8859-1和Unicode编码,而.NET Framework支持更多。

但是,通过注册System.Text.Encoding.CodePages NuGet package中的CodePagesEncodingProvider,可以扩展.NET Core以支持其他编码,如Windows-1252,Shift-JIS,GB2312。

安装NuGet软件包后,必须按照CodePagesEncodingProvider class文档中的说明执行以下步骤来注册提供程序:

  
      
  1. 将System.Text.Encoding.CodePages.dll程序集的引用添加到项目中。
  2.   
  3. 从静态Instance属性中检索CodePagesEncodingProvider对象。
  4.   
  5. 将CodePagesEncodingProvider对象传递给Encoding.RegisterProvider方法。
  6.   

答案 2 :(得分:1)

我在尝试读取 xlsx 文件并将其转换为 DataTable 时遇到了类似的问题。我发现编码 1252 在 .NET Core 中不是默认的,因此我必须单独添加 NuGet 包。

下面是我从内存流中转换数据的方法。

private static DataTableCollection ExcelToDataTable(MemoryStream stream)
{
    System.Text.Encoding.RegisterProvider(System.Text.CodePagesEncodingProvider.Instance);

    using (var reader = ExcelReaderFactory.CreateReader(stream))
    {
        var result = reader.AsDataSet(new ExcelDataSetConfiguration()
        {
            ConfigureDataTable = (data) => new ExcelDataTableConfiguration()
            {
                UseHeaderRow = true
            }
        });
        
        return result.Tables;
    }
}

我在方法开始时从 nuGet Package 中引用了 Encoder,它对我来说效果很好。这个答案来晚了,但可能会对从流中读取数据的人有所帮助。

System.Text.Encoding.RegisterProvider(System.Text.CodePagesEncodingProvider.Instance);

答案 3 :(得分:0)

nuget:
Install-Package System.Text.Encoding.CodePages -Version 5.0.0

代码:
Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);