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

C# System.NotSupportedException:没有可用于编码1252的数据,c#,C#,我正在与一家公司合作,研究如何生成一个支付令牌,允许客户使用TC Truste主机支付表单。他们的开发团队给了我一个如何检索这个令牌的示例 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 TrustCommer

我正在与一家公司合作,研究如何生成一个支付令牌,允许客户使用TC Truste主机支付表单。他们的开发团队给了我一个如何检索这个令牌的示例

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);
        }
    }
}
我在VisualStudio中创建了一个新的控制台应用程序,复制了此代码,并用正确的凭据替换了用户名和密码。当我尝试运行此命令时,控制台中出现以下错误

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


我试着用谷歌搜索这个错误,大多数的回答都有点超出我的理解。我当然不是C#专家。

.NETCore只支持ASCII、ISO-8859-1和Unicode编码,而.NETFramework支持更多

但是,.NET Core可以通过从注册
codepagensencodingprovider
扩展以支持其他编码,如Windows-1252、Shift JIS、GB2312

安装NuGet软件包后,必须执行文档中所述的以下步骤来注册提供程序:

  • 将对System.Text.Encoding.CodePages.dll程序集的引用添加到项目中
  • 从静态实例属性检索CodePagesEncodingProvider对象
  • 将CodePagesEncodingProvider对象传递给Encoding.RegisterProvider方法

  • 卡库里说的。为了清楚起见,在打开流之前,您需要以下代码行(步骤2,3):

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

    默认情况下,ExcelDataReader抛出NotSupportedException“无数据可用” 可用于编码.NET Core上的1252

    要修复此问题,请向包System.Text.Encoding.CodePages添加依赖项 然后添加代码以在过程中注册代码页提供程序 应用程序初始化(Startup.cs中的f.ex):

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

    这是解析二进制BIFF2-5 Excel文档中的字符串所必需的 用DOS时代的代码页编码。这些编码由 在完整的.NET Framework中默认,但在.NET Core中不默认


    您的控制台使用的是.NETFramework还是.NETCore?我认为在Core上,你需要安装.NETCore2.0。我确实通过nuget安装了该软件包,并重建了我的解决方案。我遇到了相同的错误。我忘记了您需要按照中所述的三个步骤进行操作。如果您确实不需要1252编码,则使用UTF8@ckuri成功了!谢谢,救了我一天!