EldoS | Feel safer!

Software components for data protection, secure storage and transfer

Create mime header from string

Also by EldoS: CallbackProcess
A component to control process creation and termination in Windows and .NET applications.
#28963
Posted: 03/26/2014 08:11:57
by Niklas Kjellander (Standard support level)
Joined: 09/30/2008
Posts: 49

Is there a good way to create an instance of TElMessageHeaderField from a string value?

The string value is the complete header field with label, value and optionally some parameters for example:
Code
"Content-type: text/html; charset=utf-8"


I've noticed that the constructor for TElMessageHeaderField does note handle any additiona parameters in the value argument such as the charset.
#28964
Posted: 03/26/2014 08:22:53
by Alexander Ionov (EldoS Corp.)

Thank you for contacting us.

Unfortunately there is no direct way to do this. TElMessageHeaderField class is not intended to be instantiated externally.

So you have 2 workarounds for this:
1. Create a TElMessageHeader instance and call its Parse method. The header object will contain only one field object which is an instance of TElMessageHeaderField class.
2. You can declare a successor class from TElMessageHeaderField class; so you will have access to its protected method Parse (or use reflections in order to call that method).


--
Best regards,
Alexander Ionov
#28965
Posted: 03/26/2014 08:40:14
by Niklas Kjellander (Standard support level)
Joined: 09/30/2008
Posts: 49

I've tried Option 1 already, but it resulted in an IndexOutOfRangeException.
I'm not sure that i really understand how to use the charset parameter. My code looked something like this:

Code
byte[] headerData = Encoding.ASCII.GetBytes("Content-Type: text/html; charset=utf-8");
byte[] charset = Encoding.ASCII.GetBytes(Encoding.ASCII.EncodingName);
int offset = 0;
TElMessageHeader mh = new TElMessageHeader();
var h = mh.Parse(headerData, ref offset, ref charset, true);


Any suggestions?
By the way I'm using the .Net edition.
#28980
Posted: 03/27/2014 03:54:47
by Alexander Ionov (EldoS Corp.)

The following code works fine:
Code
            byte[] buffer = SBUtils.__Global.BytesOfString("Content-Type: text/html; charset=utf-8\r\n");

            TElMessageHeader header = new TElMessageHeader();
            int offset = 1;
            byte[] charset = new byte[0];
            int err = header.Parse(buffer, ref offset, ref charset, false);
            Console.WriteLine("Result: {0}", err);
            Console.WriteLine("Charset: {0}", SBUtils.__Global.StringOfBytes(charset));

Please make sure you have a CRLF pair at the end of the input string. Otherwise the code does not work.


--
Best regards,
Alexander Ionov

Reply

Statistics

Topic viewed 583 times

Number of guests: 1, registered members: 0, in total hidden: 0




|

Back to top

As of July 15, 2016 EldoS Corporation will operate as a division of /n software inc. For more information, please read the announcement.

Got it!