2
votes

I know this has been asked before but the solutions suggested in those cases have not work for me. I have a simple console app that scrapes data from a website. Last night they updated their security settings so my code doesn't work anymore.

Here Is the code snippet I'm trying to use:

try
{
    ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3 | SecurityProtocolType.Tls;
    var request2 = WebRequest.Create("https://www.eex-transparency.com/") as HttpWebRequest;
    request2.Method = "GET";
    request2.Accept = "text/html, application/xhtml+xml, */*";
    request2.Headers.Set(HttpRequestHeader.AcceptLanguage, "en-gb");
    request2.Headers.Set(HttpRequestHeader.AcceptEncoding, "gzip, deflate");
    request2.Headers.Add("DNT", "1");
    request2.UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64; Trident/7.0; rv:11.0) like Gecko";
    request2.Host = "www.eex-transparency.com";
    request2.KeepAlive = true;
    request2.Headers.Set(HttpRequestHeader.CacheControl, "no-cache");
    request2.Proxy = ProxyFactory.GetWebProxy();
    using (var response = request2.GetResponse())
    {}
}
catch (Exception ex)
{ Console.WriteLine(ex);}

There error I receive is:

System.Net.WebException: The underlying connection was closed: An unexpected error occurred on a send. ---> System.IO.IOException: Authentication failed becausethe remote party has closed the transport stream.

at System.Net.Security.SslState.StartReadFrame(Byte[] buffer, Int32 readBytes, AsyncProtocolRequest asyncRequest)

at System.Net.Security.SslState.StartReceiveBlob(Byte[] buffer, AsyncProtocol Request asyncRequest)

at System.Net.Security.SslState.StartSendBlob(Byte[] incoming, Int32 count, A syncProtocolRequest asyncRequest)

at System.Net.Security.SslState.ForceAuthentication(Boolean receiveFirst, Byte[] buffer, AsyncProtocolRequest asyncRequest)

at System.Net.Security.SslState.ProcessAuthentication(LazyAsyncResult lazyResult)

at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)

at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)

at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)

at System.Net.TlsStream.ProcessAuthentication(LazyAsyncResult result)

at System.Net.TlsStream.Write(Byte[] buffer, Int32 offset, Int32 size)

at System.Net.ConnectStream.WriteHeaders(Boolean async)

--- End of inner exception stack trace ---

Any help will be greatly appreciated.

Thanks in advance.

2
That URL supports TLS v1.1 / v1.2 only, try adjusting your SecurityProtocol mask accordinglyAlex K.
Thanks @AlexK. that worked, for my education how did you find out the url only supports v1.1/1.2? For anyone who comes across this tls11 and tls12 are available in framework 4.5 and higher.Elias
Your welcome. I ran the URL through sslanalyzer.comodoca.comAlex K.
@Alex K. Ok thanks one for me to use in the future.Elias
If you fixed the issue you can post what you did as an answer then accept it.Alex K.

2 Answers

8
votes

This specific url only supports TLS version 1.1 and 1.2. Added the below for the code to work:

ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls11;

Make sure you are using Framework 4.5 or higher

-1
votes

Do one thing it will work

request2.KeepAlive = false;