System.NotSupportedException:没有数据可用于编码1252 [英] System.NotSupportedException: No data is available for encoding 1252

查看:102
本文介绍了System.NotSupportedException:没有数据可用于编码1252的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在与 Trust Commerce教程进行合作,以了解如何生成可让客户进行交易的支付令牌使用TC受托人托管人付款表格.他们的开发团队为我提供了一个有关如何检索此令牌的示例.

I'm working with a Trust Commerce Tutorial on how to generate a payment token that will allow customers to use the TC Trustee Host payment form. I was given an example on how to retrieve this token by their dev team.

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

I made a new console application in visual studio, copied this code, and replaced the username and password with the correct credentials. When I try to run this, I get the following error in the console.

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

System.NotSupportedException: No data is available for encoding 1252. For information on defining a custom encoding, see the documentation for the Encoding.RegisterProvider method. at System.Text.Encoding.GetEncoding(Int32 codepage) at TCToken.Program.Main(String[] args) in C:\Users\xxxx\source\repos\TCToken\TCToken\Program.cs:line 29

我已经尝试通过Google搜索此错误,并且大多数回复都超出了我的理解范围.我当然不是C#专家.

I've tried to google this error and most of the responses are a little above my understanding. I'm certainly not a C# expert.

推荐答案

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

.NET Core supports only ASCII, ISO-8859-1 and Unicode encodings, whereas .NET Framework supports much more.

但是,可以通过从CodePagesEncodingProvider 来扩展.NET Core以支持Windows-1252,Shift-JIS,GB2312等其他编码..org/packages/System.Text.Encoding.CodePages/"rel =" noreferrer> System.Text.Encoding.CodePages NuGet包.

However, .NET Core can be extended to support additional encodings like Windows-1252, Shift-JIS, GB2312 by registering the CodePagesEncodingProvider from the System.Text.Encoding.CodePages NuGet package.

在安装NuGet软件包之后,按照

After the NuGet package is installed the following steps as described in the documentation for the CodePagesEncodingProvider class must be done to register the provider:

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

这篇关于System.NotSupportedException:没有数据可用于编码1252的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆