0

I am trying to use the HttpWebRequestclass in order to get a page's html content. The page address is started with https so it means I need to establish the connection over SSL protocol. Now the problem is, when I run my code block, it throws an exception saying :

Unable to read data from the transport connection: An existing connection was forcibly closed by the remote host. system.net.sockets.socketexception

My Code

var request = (HttpWebRequest)WebRequest.Create("https://www.***.com/");
var response = (HttpWebResponse)request.GetResponse();

What I'v tried to solve the problem

I think my code lacks of something which unfortunately I don't know what exactly it is! I changed the URL's content and used a http link and the code ran well. I'v read a lot of links and tried different ideas but it still doesn't work.

My questions

Would anyone tell me if it is possible to Get the content of a https address? If it is possible, how could I achieve it? And if it is not, how do the internet browsers explore the address while they are nothing more than blocks of code written by some programmers?!

Retro Code
  • 145
  • 1
  • 9
  • Can you get this specific url with postman or in your browser? Do you have control over the other end of the connection? `HttpWebRequest` should be perfectly capable of handling also https requests if the server is setup correctly – derpirscher Aug 09 '18 at 09:33
  • @derpirscher I have nor control over the other end of the connection. I am actually trying to download the content of a website in order to interpret it's content for personal use. Do you have any idea? – Retro Code Aug 09 '18 at 09:38
  • @derpirscher Yes I do explore the address using the Chrome. I'v also read the link you'v mentioned but it didn't help me – Retro Code Aug 09 '18 at 09:41
  • Does loading this website in the browser give any errors (like wrong certificate or something like that)? Did you try the answers to the question I linked in my comment? Otherwise it's hard to guess for reasons without knowing the website you are trying to access – derpirscher Aug 09 '18 at 09:42
  • If it works with http but not with https it quite likely is an issue with the certificate or the tls handshake. Ignoring certificate errors: https://stackoverflow.com/a/2675183/3776927, Disable old TLS v1.0 https://stackoverflow.com/a/45692875/3776927 – derpirscher Aug 09 '18 at 09:47
  • You can also check which version of TLS is used in Chrome. When the page is loaded: https://security.stackexchange.com/a/120778 – derpirscher Aug 09 '18 at 09:53
  • @derpirscher I got the response dude! What was the problem?! Thank you very much! – Retro Code Aug 09 '18 at 09:54
  • Depends on what you added to your code. If you just ignored certificate errors, it was a faulty certificate (expired, wrong domain ...). If you set the TLS version, the server you are accessing might not support TLS v1.0 any more, but this is still the default on many .NET environments. – derpirscher Aug 09 '18 at 09:56

0 Answers0