GetRequestStream throwing InvalidOperationException

The piece of codes below works fine for dotnetcore, but throwing exception in TinyCLR.
This is a simple code to do post http.
Let me know what went wrong, and perhaps there is work around.

    try
    {
        using (var req = HttpWebRequest.Create(url) as HttpWebRequest)
        {
            req.Method = "POST";
            string postData = "password=000000000000";
            byte[] data = Encoding.UTF8.GetBytes(postData);
            req.ContentType = "application/x-www-form-urlencoded; charset=UTF-8";
            req.ContentLength = data.Length;
            req.Accept = "application/json";
            req.KeepAlive = true;
            //req.HttpsAuthentCerts = certx509;
            req.ProtocolVersion = HttpVersion.Version10;
            req.ReadWriteTimeout = 2000;

            Stream reqStream = req.GetRequestStream();
            reqStream.Write(data, 0, data.Length);
            reqStream.Close();

            using (var res = req.GetResponse() as HttpWebResponse)
            {
                Debug.WriteLine("status code = " + res.StatusCode);
                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);
                }
            }
        }
    }

    catch {
    }

This is the error:
DoTestHttps
#### Exception System.InvalidOperationException - CLR_E_INVALID_OPERATION (1) ####
#### Message:
#### GHIElectronics.TinyCLR.Devices.Network.Provider.NetworkControllerApiWrapper::AuthenticateAsClient [IP: 0000] ####
#### System.Net.Security.SslStream::AuthenticateAsClient [IP: 0016] ####
#### System.Net.Security.SslStream::AuthenticateAsClient [IP: 0009] ####
#### System.Net.Security.SslStream::AuthenticateAsClient [IP: 0008] ####
#### System.Net.Security.SslStream::AuthenticateAsClient [IP: 0006] ####
#### System.Net.HttpWebRequest::SubmitRequest [IP: 0019] ####
#### System.Net.HttpWebRequest::GetRequestStream [IP: 0008] ####
#### TinyCLRApplication.Program::Main [IP: 0128] ####
Exception thrown: ‘System.InvalidOperationException’ in GHIElectronics.TinyCLR.Networking.Http.dll

Never mind, I just needed to uncomment //req.HttpsAuthentCerts = certx509;

1 Like