Tobe
Tobe

Reputation: 198

C#: byte 0 does not get recognized as null value

i'm sending data via a post-request to my IIS. Everything is working fine, but now I realizied that I cannot send null values to the IIS. No matter what I try, the respond data is not recognized as a null.

Here is the post-data sniffed with wireshark:

Encapsulated multipart part: 
Content-Disposition: form-data; name="@Beschreibung"\n\r\n
Data (1 byte)
Data: 00
[Length: 1]
Boundary: \r\n--*****\r\n

And here my code that is inside my IIS webmethod:

            HttpContext postedContext = HttpContext.Current;

            var bytes = Encoding.Default.GetBytes(postedContext.Request.Form["@Beschreibung"]);

            var beschreibung = Encoding.UTF8.GetString(bytes);
            log.Info("BYTE ARRAY START");
            foreach (byte b in bytes)
            {
                log.Info(b);
            }
            log.Info("BYTE ARRAY ENDE");

            log.Info("Beschreibung: |" + beschreibung + "|");
            if (beschreibung == null)
                log.Info("IST NULL");

            if (string.IsNullOrEmpty(beschreibung))
                log.Info("IST NULL OR EMPTY");

            if (string.IsNullOrWhiteSpace(beschreibung))
                log.Info("IST NULL OR WHITESPACE");

And the log output:

2018-07-31 10:59:09,826 [24] INFO - BYTE ARRAY START
2018-07-31 10:59:09,826 [24] INFO - 0
2018-07-31 10:59:09,826 [24] INFO - BYTE ARRAY ENDE
2018-07-31 10:59:09,826 [24] INFO - Beschreibung: | |

As you can see, the data in whireshark is only one byte and that is 0. The bytearray representation of the data in the C# code inside my webmethod is still only one byte that is 0. BUT the string itself is not null, nor an empty string, it is not even a whitespace, because the code doesn't run into a single of the if-statements.

What am I missing here? I need to recognize this string as a null, so I can write DBNull.Value inside my database and not " " or "".

Any help is appreciated, thanks in advance.

Edit: Okay, understood. Null is a valid char... Here is the sending site (in Xamarin.iOS), I might better start a new topic and ask the Xamarin guys how to send null.

var postBody = new NSMutableData();
        postBody.AppendData(new NSString(twoHyphens + boundary + lineEnd).Encode(NSStringEncoding.UTF8));
        postBody.AppendData(new NSString("Content-Disposition: form-data; name=\"@Beschreibung\"" + crlf).Encode(NSStringEncoding.UTF8));
        postBody.AppendData(new NSString(lineEnd).Encode(NSStringEncoding.UTF8));
        postBody.AppendData(new NSString("\0").Encode(NSStringEncoding.UTF8));
        postBody.AppendData(new NSString(lineEnd).Encode(NSStringEncoding.UTF8));
        postBody.AppendData(new NSString(twoHyphens + boundary + lineEnd).Encode(NSStringEncoding.UTF8));

In line 5 I send the data as "\0". Earlier I sent it with

byte zero = 0;
postbody.AppendBytes(new byte[]{zero};

So to only send a byte 0, but that's of course the same problem...

Upvotes: 0

Views: 3858

Answers (2)

Damien_The_Unbeliever
Damien_The_Unbeliever

Reputation: 239694

If you want to send an empty string - send an empty string:

postBody.AppendData(new NSString("").Encode(NSStringEncoding.UTF8));

As I said in another comment, "strings are arrays of characters terminated by a null character" is a (feature/mistake) of and most more recent languages recognize that a null character is perfectly valid within a string.

Upvotes: 0

Thomas Voß
Thomas Voß

Reputation: 1165

this is because the sting is not null nor empty. It contains a single char ('\0')

Null ('\0') is a perfectly OK character thus this behaviour is correct.

You might test for it by comparing your string with beschreibung == "\0"

Thomas

Upvotes: 4

Related Questions