By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,293 Members | 1,397 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,293 IT Pros & Developers. It's quick & easy.

POST HttpWebRequest to SLL Problem

P: n/a
TJO
Can someone at MS please reply to this. I am trying to post data so a web
form via ssl with the following code. I keep getting this error: "The
underlying connection was closed: Could not establish secure channel for
SSL/TLS"

private void mainHttpCalls(string postData)
{
HttpWebRequest objRequest1 ;
HttpWebRequest objRequest2 ;

HttpWebResponse objResponse1;
HttpWebResponse objResponse2;

StreamReader sr = null;
StreamWriter myWriter = null;

CookieCollection cookies = null;

string result;

clearTextboxes();

Encoding utf8 = new UTF8Encoding();

ServicePointManager.CertificatePolicy = new AcceptAllCertificatePolicy();

string uri = cmbo_uri.Text;
txt_postdata.Text = postData;
postData = HttpUtility.UrlEncode(postData);

// Get Logon Page
objRequest1 = (HttpWebRequest)System.Net.HttpWebRequest.Create(u ri);
objRequest1.Method = "GET";
objRequest1.ContentType = "text/html";
objRequest1.CookieContainer = new CookieContainer();

// Display request header, cookies in left textbox displays
txt_requestheader.Text = objRequest1.Headers.ToString();
foreach(Cookie c in
objRequest1.CookieContainer.GetCookies(objRequest1 .RequestUri))
{
txt_requestcookies.Text += c.Name + " = " + c.Value + "\n";
}

// Get the response from the request
try
{
objResponse1 = (HttpWebResponse)objRequest1.GetResponse();

// Save the session and other cookies from the initial request.
if(objResponse1.Cookies.Count > 0)
cookies = objResponse1.Cookies;

sr = new StreamReader(objResponse1.GetResponseStream(), utf8);
result = sr.ReadToEnd();

sr.Close();
objResponse1.Close();

// Display response header, cookies in left textbox displays
txt_responseheader.Text = objResponse1.Headers.ToString();
foreach(object c in objResponse1.Cookies)
{
txt_responsecookies.Text += c.ToString();
}

txt_response.Text = result;
}
catch (Exception er1)
{
txt_errors.Text = er1.ToString();
return;
}

// ******************** Now post the data **************************
// ******************** Now post the data **************************

result = string.Empty;
sr = null;

byte[] postcontent = utf8.GetBytes(postData);

objRequest2 = (HttpWebRequest)System.Net.HttpWebRequest.Create(u ri);
objRequest2.ContentType = "application/x-www-form-urlencoded";
objRequest2.ContentLength = postcontent.Length;
objRequest2.KeepAlive = true;
objRequest2.CookieContainer = new CookieContainer();
if(cookies != null)
objRequest2.CookieContainer.Add(cookies);
objRequest2.Method = "POST";

// Sending the post request via stream writer
try
{

myWriter = new StreamWriter(objRequest2.GetRequestStream(), utf8);

myWriter.Write(utf8.GetString(postcontent),0,utf8. GetString(postcontent).Len
gth);
myWriter.Flush();
myWriter.Close();

objResponse2 = (HttpWebResponse)objRequest2.GetResponse();
StreamReader sr2 = new StreamReader(objResponse2.GetResponseStream(),
utf8);
result = sr2.ReadToEnd();

objResponse2.Close();
sr2.Close();
}
catch (Exception e2)
{
txt_errors.Text = "Error Writing Request: " + e2.Message + "\n" +
e2.ToString();
return;
}
// Get the response from the request
try
{
// Display response header, cookies in righ textbox displays
txt_responseheader2.Text = objResponse2.Headers.ToString();
foreach(object c in objResponse2.Cookies)
{
txt_responsecookies2.Text += c.ToString();
}

txt_response2.Text = result;

}
catch (Exception re)
{
txt_errors2.Text = "Error Getting Response: " + "\n" + re.ToString();
}
// Display request header, cookies in righ textbox displays
txt_requestheader2.Text = objRequest2.Headers.ToString();
foreach(Cookie c in
objRequest2.CookieContainer.GetCookies(objRequest2 .RequestUri))
{
txt_requestcookies2.Text += c.Name + " = " + c.Value + "\n";
}

}

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

}
Jul 19 '05 #1
Share this question for a faster answer!
Share on Google+

This discussion thread is closed

Replies have been disabled for this discussion.