HttpWebResponse.StatusCode throws Exception

The snippet below works fine until the very last line where I tried to read the StatusCode.
It threw exception.

I was able to read the response content correctly, even StatusDescription is read correctly.
The actual returned http status from the server is 200.
From the internal code, this seems to suggest it’s not implemented?

            using (var res = req.GetResponse() as HttpWebResponse)
            {
                using (var stream = res.GetResponseStream())
                {
                    do
                    {
                        read = stream.Read(result, 0, result.Length);
                        total += read;

                        System.Diagnostics.Debug.WriteLine("read : " + read);
                        System.Diagnostics.Debug.WriteLine("total : " + total);

                        var page = new String(System.Text.Encoding.UTF8.
                            GetChars(result, 0, read));

                        System.Diagnostics.Debug.WriteLine("Response : " + page);
                    }

                    while (read != 0);

                    Debug.WriteLine("status desc = " + res.StatusDescription);
                    Debug.WriteLine("status code = " + res.StatusCode);
                }
            }
        }

Here is the exeception:
DoTestHttps
read : 54
total : 54
Response : {“flag”: “flag{1b17b7465163a52dbd6c10175f84d273}”}
read : 0
total : 54
Response :
status desc = OK
#### Exception System.Exception - CLR_E_UNSUPPORTED_INSTRUCTION (1) ####
#### Message:
#### TinyCLRApplication.Program::DoTestHttps [IP: 0134] ####
#### TinyCLRApplication.Program::Main [IP: 0128] ####
Exception thrown: ‘System.Exception’ in TinyCLRApplication.exe

@Gus_Issa, I think I found the cause of the issue.
TinyCLR does not support conversion from enum HttpStatusCode to string.
I can reduce the issue to the following codes:

var scode = HttpStatusCode.OK;
Debug.WriteLine(scode.ToString());

The ToString() call will throw the same exception.
I can workaround this issue.

1 Like

cast enum to object type will work.

2 Likes

Ok, just confirmed that would work too.

1 Like