SSL WebRequest Error Help Please

  • Thread starter Thread starter TJO
  • Start date Start date
T

TJO

Why do I keep getting the following error.

"The underlying connection was closed: Could not establish secure channel
for SSL/TLS."

I am trying to log into a SSL site with the following code. What am i
missing??


// Post Credentials
string postData = "uid=myuserid&currpassword=mypassword";
StreamWriter myWriter = null;

WebResponse objResponse2;
WebRequest objRequest2 = System.Net.HttpWebRequest.Create(cmbo_uri.Text);
objRequest2.Method = "POST";
objRequest2.ContentLength = postData.Length;
objRequest2.ContentType = "application/x-www-form-urlencoded";
objRequest2.Timeout = 100000;

ServicePointManager.CertificatePolicy= new AcceptAllCertificatePolicy();

try
{
myWriter = new StreamWriter(objRequest2.GetRequestStream());
myWriter.Write(postData);

}
catch (Exception e2)
{
txt_errormsg.Text = "Error Writing Request: " + e2.Message;
}
finally
{
if(myWriter != null)
myWriter.Close();
}

try
{
objResponse2 = (HttpWebResponse)objRequest2.GetResponse();

using (StreamReader sr = new
StreamReader(objResponse2.GetResponseStream()) )
{
result = sr.ReadToEnd();

// Close and clean up the StreamReader
sr.Close();
}

txt_header.Text = objResponse2.Headers.ToString();
txt_response.Text = result;
}
catch (Exception re)
{
txt_errormsg.Text = "Error Getting Response: " + re.Message;
}


sealed class AcceptAllCertificatePolicy : ICertificatePolicy
{
public AcceptAllCertificatePolicy()
{
//
// TODO: Add constructor logic here
//
}

public bool CheckValidationResult(ServicePoint srvPoint,
X509Certificate certificate, WebRequest request,
int certificateProblem)
{
// Just accept.
return true;
}

}
 
TJO said:
Why do I keep getting the following error.
"The underlying connection was closed: Could not establish secure channel
for SSL/TLS."
I am trying to log into a SSL site with the following code. What am i
missing??

The server probably requested client authentication and you haven't provided
any client certificate to send to the server. Look at the
HttpWebRequest.ClientCertificates to solve that problem.
The same error could also be thrown for various other reasons [for instance,
if your computer only has weak cryptography support and the server requires
strong crypto, the SSL/TLS handshake will fail], however the client
certificates thingy mentioned above is the most likely problem.

Regards,
Pieter Philippaerts
http://www.mentalis.org/
 
Back
Top