9

I am currently trying to get a response from a server that is using SSL in C#. I have the code to do this in Java, but it looks like they do not translate 1:1.

I do have some code that I found that works for regular pages, but not for the one I need (maybe because of the SSL thing). Here is the code:

        WebRequest request = WebRequest.Create("https://" + sslServerHost + ":" + sslServerPort);
    request.Proxy = null;
    request.Credentials = CredentialCache.DefaultCredentials;

    HttpWebResponse response = (HttpWebResponse)request.GetResponse();
    Stream dataStream = response.GetResponseStream();
    StreamReader reader = new StreamReader(dataStream);
    string responseFromServer = reader.ReadToEnd();

UPDATE: Sorry I seem to have forgotten what the error is. I'm getting a protocol violation exception at the "HttpWebResponse response = (HttpWebResponse)request.GetResponse(); " line. Any ideas? Thanks guys.

  • Amber: Can you describe what's going wrong? Is it just not returning results? Are you getting some sort of exception? If so, what kind of exception, and what is the message? – JMarsch Apr 02 '09 at 14:54
  • 1
    Could potentially be an untrusted certificate issue? – Dscoduc Apr 22 '09 at 17:55

3 Answers3

14

HTTP conversations over SSL use a properly issued certificate for validation.

You could use the RemoteCertificateValidationCallback delegate for validating the SSL certificate as follows:

public static void ConnectSSL()
{

    WebRequest request = WebRequest.Create("https://" + sslServerHost + ":" + sslServerPort);
    request.Proxy = null;
    request.Credentials = CredentialCache.DefaultCredentials;

   //allows for validation of SSL certificates 

    ServicePointManager.ServerCertificateValidationCallback += new  System.Net.Security.RemoteCertificateValidationCallback(ValidateServerCertificate);

    HttpWebResponse response = (HttpWebResponse)request.GetResponse();
    Stream dataStream = response.GetResponseStream();
    StreamReader reader = new StreamReader(dataStream);
    string responseFromServer = reader.ReadToEnd();

}

//for testing purpose only, accept any dodgy certificate... 
public static bool ValidateServerCertificate(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors)
{
          return true; 
}
amit-agrawal
  • 1,563
  • 2
  • 13
  • 24
2

Just an idea, but have you tried it with a final "/"? Also - you might find this approach easier:

string s;
using(WebClient client = new WebClient()) {
    client.UseDefaultCredentials = true;
    s = client.DownloadString("https://" + sslServerHost + ":"
       + sslServerPort + "/");
}
Marc Gravell
  • 1,026,079
  • 266
  • 2,566
  • 2,900
1

Here is some test code that I have used successfully for testing SSL connections...

HttpWebRequest request = (HttpWebRequest)WebRequest.Create("https://www.dscoduc.com");
//request.Method = "HEAD";
//request.AllowAutoRedirect = false;
request.Credentials = CredentialCache.DefaultCredentials;

// Ignore Certificate validation failures (aka untrusted certificate + certificate chains)
ServicePointManager.ServerCertificateValidationCallback = ((sender, certificate, chain, sslPolicyErrors) => true); 

HttpWebResponse response = (HttpWebResponse)request.GetResponse();
Stream resStream = response.GetResponseStream();
StreamReader reader = new StreamReader(resStream);
string responseFromServer = reader.ReadToEnd();
Dscoduc
  • 7,714
  • 10
  • 42
  • 48