Cannot access a disposed object named System.Net.TlsStream

M

Mackuack

Hello everybody

i have the following code to access an application via https:

---------------------------------------------
System.Net.ServicePointManager.CertificatePolicy = new
TrustAllCertificatePolicy();

httpWebRequest = (HttpWebRequest)WebRequest.Create(url);

X509Certificate cer =
X509Certificate.CreateFromCertFile(@"c:\CertificadoCliente.cer");

httpWebRequest.ClientCertificates.Add(cer);
httpWebRequest.Method = "POST";
httpWebRequest.ContentLength = stringPost.Length;

streamWriter = new StreamWriter(httpWebRequest.GetRequestStream());

streamWriter.Write(stringPost);
streamWriter.Close();

HttpWebResponse httpWebResponse =
(HttpWebResponse)httpWebRequest.GetResponse();
StreamReader streamReader = new
StreamReader(httpWebResponse.GetResponseStream());
respuesta = streamReader.ReadToEnd();
streamReader.Close();
---------------------------------------------

Although the connection is created (i think the certificates are
correctly installed), i got an exception when i try to close the
streamWriter:

"Cannot access a disposed object named \"System.Net.TlsStream\"."

As you can see i have created the TrustAllCertificatePolicy class, but
i still get that exception. Could anyone help me?

Thanks in advance. Patricio.
 
T

Tomas Restrepo \(MVP\)

Mackuack,
Hello everybody

i have the following code to access an application via https:

---------------------------------------------
System.Net.ServicePointManager.CertificatePolicy = new
TrustAllCertificatePolicy();

httpWebRequest = (HttpWebRequest)WebRequest.Create(url);

X509Certificate cer =
X509Certificate.CreateFromCertFile(@"c:\CertificadoCliente.cer");

httpWebRequest.ClientCertificates.Add(cer);
httpWebRequest.Method = "POST";
httpWebRequest.ContentLength = stringPost.Length;

streamWriter = new StreamWriter(httpWebRequest.GetRequestStream());

streamWriter.Write(stringPost);
streamWriter.Close();

You're closing the StreamWriter, which closes the underlying request stream.
You never get to actually listen for the response! Don't close the writer
(just flush it) until you're done with the request and have received a
response.
 
M

Mackuack

You're closing the StreamWriter, which closes the underlying request stream.
You never get to actually listen for the response! Don't close the writer
(just flush it) until you're done with the request and have received a
response.

Really?

I have found the solution by myself, the problem was that i have the
client certificate installed only for the machine account. I have also
installed it for the current user and now all it´s working fine
without changing anything in the code.

Thanks a lot for your help.

MacKuack
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,990
Messages
2,570,211
Members
46,796
Latest member
SteveBreed

Latest Threads

Top