Lucas Perrett Lucas Perrett - 6 days ago 6
C# Question

Decode Base64 HTTPWEBRESPONSE in C# - Returns Japanese

I am using an SSIS Script Component to execute C# code for a WEB API that is returning me a value that is a Base64 Encoded byte un a return array.

The actual return value is an encoded JSON string which I subsequently need to deserialize and output.

I am using the following C# to try and decode the response.

foreach (Attachment atch in rptOutputResponse.Response.attachments)
{
RptAttachmentsBuffer.AddRow();

RptAttachmentsBuffer.AppId = rptOutputResponse.Response.appId;
RptAttachmentsBuffer.Type = atch.type;
RptAttachmentsBuffer.Name = atch.name;
RptAttachmentsBuffer.ContentType = atch.contentType;

byte[] rptConRaw = Encoding.UTF8.GetBytes(atch.content);

String s = Convert.ToBase64String(rptConRaw);

byte[] newbytes = Convert.FromBase64String(s);

System.Windows.Forms.MessageBox.Show(BitConverter.ToString(newbytes));

RptAttachmentsBuffer.ReportContent.AddBlobData(newbytes);

}


The expected result looks like this.

{"Applications":{"Application":{"AppID":2891426,"ReportID":4160202,"AppReference":"Taplin 12-Oct-16 10:37:02AM","CreateDT":"2016-10-12 10:37:23.3500000","ClientName":"GoGetta Brisbane","StoreName":"Brokers","Email":"","StoreCode":"GGT08","AppShortReference":"02","ClientNameShort":"GGT Bris","StoreNameShort":"GGT08","VerifyEmployer":null,"VerifyAmount":null,"VerifyFrequency":null,"VerifyWeekday":null,"LocalityCode":"en_AU","TemplateReportID":12,"daysRange":90,"templateReportName":"Enhanced Income Liabilities Full Report","Accounts":{"Account":[{"AccountID":4829641,"AccountNumber":"17-986-7500","AccountType":"savings","AccountName":"XXXXXXXXXXXX7500","AccountHolder


However when output to my table it is coming through in Japanese. What am I doing wrong?

Answer

Please try the following:

foreach (Attachment atch in rptOutputResponse.Response.attachments)
{
    RptAttachmentsBuffer.AddRow();

    RptAttachmentsBuffer.AppId = rptOutputResponse.Response.appId;
    RptAttachmentsBuffer.Type = atch.type;
    RptAttachmentsBuffer.Name = atch.name;
    RptAttachmentsBuffer.ContentType = atch.contentType;

    string json = Encoding.UTF8.GetString(Convert.FromBase64String(atch.content));

    System.Windows.Forms.MessageBox.Show(json);

    RptAttachmentsBuffer.ReportContent.AddBlobData(newbytes);

}
Comments