Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

System.NotSupportedException: No data is available for encoding 1252

Tags:

c#

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);         }     } } 

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: 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

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.

like image 644
onTheInternet Avatar asked Jun 14 '18 13:06

onTheInternet


People also ask

What is the error code for encoding 1252?

Here is the code: Unhandled Exception: 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)

How do I enable 1252 encoding in exceldatareader?

By default, ExcelDataReader throws a NotSupportedException "No data is available for encoding 1252.". on .NET Core. To fix, add a dependency to the package System.Text.Encoding.CodePages and then add code to register the code page provider during application initialization (f.ex in Startup.cs):

Is Windows-1252 encoding required to read a file?

Important thing in the code above is using Windows-1252 encoding when reading contents of file. With out specifying encoding it is expected that file is in UTF-8 already. Liked this post? Empower your friends by sharing it!

How to add Windows-1252 encodings to NuGet?

It turns out that some “exotic” encodings (including popular Windows-1252) are defined in separate NuGet package and these encodings are not available by default. Solution is simple. Add System.Text.Encoding.CodePages NuGet package to solution and use the following piece of code in application startup class to registester new encodings.


1 Answers

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

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.

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. Add a reference to the System.Text.Encoding.CodePages.dll assembly to your project.
  2. Retrieve a CodePagesEncodingProvider object from the static Instance property.
  3. Pass the CodePagesEncodingProvider object to the Encoding.RegisterProvider method.
like image 112
ckuri Avatar answered Sep 24 '22 16:09

ckuri