Quantcast
Channel: Common Language Runtime Internals and Architecture forum
Viewing all articles
Browse latest Browse all 1710

SSL Handshake on HttpWebRequest after WebExceptionStatus.ProtocolError

$
0
0

Hi,

I need help with ssl handshakes.
I appreciate any help you can provide :)

Tested Framework(s): .NET 4.5 (will be used), .NET 4.0 (same behavior)

Situation: 

- The following code is producing the following output

- On each request, a ssl handshake will be done
    - I checked this with ServerCertificateValidationCallback
    - and also with WireShark (Client Hello, Server Hello)

- For testing purpose, the part HandleResponse(request); was used several times. The end product will not get a response, because I dont really need it! So the method HandleResponse(request); wont be called
    - I noticed, whenever an WebException in HandleResponse(request); occurs, the next request will do a ssl handshake. The problem is, its pretty often
    - Main reason for WebException is the HTTP 502 StatusCode

- The request will be done every couple of minutes up to every couple of hours

- My goal is to minimize the ssl handshake data traffic and latency

Console Output:

Certificate
WebExceptionStatus.ProtocolError
Certificate
WebExceptionStatus.ProtocolError
Certificate
WebExceptionStatus.ProtocolError

Representing Code, if you need more information, just ask:

public static bool AcceptAllCertifications(object sender, X509Certificate certification, X509Chain chain, SslPolicyErrors sslPolicyErrors)
{
    Console.WriteLine("Certificate");
    return true;
}

public void Run()
{
    ServicePointManager.ServerCertificateValidationCallback = AcceptAllCertifications;

    while (true)
    {
        HttpWebRequest request = (HttpWebRequest)WebRequest.Create("https://####.##/path");
        request.Method = "POST";
        request.KeepAlive = true;
        request.ContentType = "application/x-www-form-urlencoded; charset=UTF-8";
        request.Proxy = null;

        string content = "...";
        byte[] byteArray = Encoding.UTF8.GetBytes(content);
        request.ContentLength = byteArray.Length;

        using (Stream stream = request.GetRequestStream())
        {
            stream.Write(byteArray, 0, byteArray.Length);
        }

        HandleResponse(request);
        
        Thread.Sleep(3000);
    }
}

public void HandleResponse(HttpWebRequest request)
{
    try
    {
        HttpWebResponse response = (HttpWebResponse)request.GetResponse();
        //...
    }
    catch (WebException e)
    {
        if (e.Status == WebExceptionStatus.ProtocolError)
        {
            using (StreamReader sr = new StreamReader(e.Response.GetResponseStream()))
            {
                string content = sr.ReadToEnd();
            }
        }
    }
}





Viewing all articles
Browse latest Browse all 1710

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>