gpt4 book ai didi

c# - 系统不支持异常: No data is available for encoding 1252

转载 作者:行者123 更新时间:2023-12-01 21:50:33 24 4
gpt4 key购买 nike

我正在与 Trust Commerce Tutorial 合作,研究如何生成允许客户使用 TC Trustee Host 付款表格的付款 token 。他们的开发团队向我提供了如何检索此 token 的示例。

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: No data is available for encoding 1252.For information on defining a custom encoding, see the documentationfor the Encoding.RegisterProvider method. atSystem.Text.Encoding.GetEncoding(Int32 codepage) atTCToken.Program.Main(String[] args) inC:\Users\xxxx\source\repos\TCToken\TCToken\Program.cs:line 29

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

最佳答案

库里说的话。需要明确的是,在打开流之前您需要以下代码行(步骤 2,3):

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

ExcelDataReader - Important note on .NET Core

By default, ExcelDataReader throws a NotSupportedException "No data isavailable for encoding 1252." on .NET Core.

To fix, add a dependency to the package System.Text.Encoding.CodePagesand then add code to register the code page provider duringapplication initialization (f.ex in Startup.cs):

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

This is required to parse strings in binary BIFF2-5 Excel documentsencoded with DOS-era code pages. These encodings are registered bydefault in the full .NET Framework, but not on .NET Core.

关于c# - 系统不支持异常: No data is available for encoding 1252,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50858209/

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