title: SSL/TLS connection not working in webclient after being in background
body: <p>So if I use my program normally no error occurs. But if it is in background for longer time and I use the webclient to visit a web address it immidiately throws <code>could not create a SSL/TLS secure channel</code> error but if I try again the error dissapears. I already set the TLS to 1.2 and expect100continue to false. </p>
  
  <pre><code>Imports System.Net
  Imports System.Text.RegularExpressions
  
  Public Class CinemaLinker
      Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
          MaximizeBox = False
          If Not HaveInternetConnection() Then
              MessageBox.Show("Warning: no internet connection!")
          End If
          ServicePointManager.Expect100Continue = False
          ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12
      End Sub
  
      Public Property TextFromBox As String
      Public Property StatusLink As String
      Private Client As New WebClient
  Private GoogleSearch = "****"
  Private BingSearch = "*****"
  Private AskSearch = "*****"
  
  Private Sub Search() Handles Button1.Click
      Cursor.Current = Cursors.WaitCursor
      TextFromBox = RichTextBox1.Text
      If Button3.Enabled And Button4.Enabled Then
          Cursor.Current = Cursors.[Default]
          MessageBox.Show("Please choose a category!")
          Exit Sub
      End If
      If TextFromBox.Contains("Type movie or tv series name here") Or String.IsNullOrEmpty(TextFromBox) Or String.IsNullOrWhiteSpace(TextFromBox) Then
          Cursor.Current = Cursors.[Default]
          If Not Button3.Enabled Then
              MessageBox.Show("Please type the name of the movie!")
              Exit Sub
          End If
          If Not Button4.Enabled Then
              If Not (String.IsNullOrEmpty(RichTextBox3.Text) Or String.IsNullOrWhiteSpace(RichTextBox3.Text) Or RichTextBox3.Text.Contains("E")) Then
                  Cursor.Current = Cursors.[Default]
                  MessageBox.Show("Please type the name of the tv series! Episode number missing!")
                  Exit Sub
              End If
              If String.IsNullOrEmpty(RichTextBox2.Text) Or String.IsNullOrWhiteSpace(RichTextBox2.Text) Or RichTextBox2.Text.Contains("S") Then
                  Cursor.Current = Cursors.[Default]
                  MessageBox.Show("Please type the name of the tv series! Season and episode numbers missing!")
                  Exit Sub
              End If
              Cursor.Current = Cursors.[Default]
              MessageBox.Show("Please type the name of the tv series!")
              Exit Sub
          End If
      End If
      If Not Button4.Enabled Then
          If String.IsNullOrEmpty(RichTextBox3.Text) Or String.IsNullOrWhiteSpace(RichTextBox3.Text) Or RichTextBox3.Text.Contains("E") Then
              If String.IsNullOrEmpty(RichTextBox2.Text) Or String.IsNullOrWhiteSpace(RichTextBox2.Text) Or RichTextBox2.Text.Contains("S") Then
                  Cursor.Current = Cursors.[Default]
                  MessageBox.Show("Season and episode numbers missing!")
                  Exit Sub
              End If
              Cursor.Current = Cursors.[Default]
              MessageBox.Show("Episode number missing!")
              Exit Sub
          ElseIf String.IsNullOrEmpty(RichTextBox2.Text) Or String.IsNullOrWhiteSpace(RichTextBox2.Text) Or RichTextBox2.Text.Contains("S") Then
              Cursor.Current = Cursors.[Default]
              MessageBox.Show("Season number missing!")
              Exit Sub
          End If
      End If
      If Not HaveInternetConnection() Then
          Cursor.Current = Cursors.[Default]
          MessageBox.Show("No internet connection!")
          Exit Sub
      End If
      Dim ImdbURL As String = getIMDbUrl(TextFromBox)
      If Not ImdbURL.Contains("******") And Not Button3.Enabled Then
          ImdbURL = getIMDbUrl(TextFromBox) + " movie"
          If Not ImdbURL.Contains("***") And Not Button3.Enabled Then
              Cursor.Current = Cursors.[Default]
              MessageBox.Show("Movie not found! Please add the year in the name!")
              Exit Sub
          End If
      End If
      If Not ImdbURL.Contains("***") And Not Button4.Enabled Then
          ImdbURL = getIMDbUrl(TextFromBox) + " TV series"
          If Not ImdbURL.Contains("****") And Not Button4.Enabled Then
              Cursor.Current = Cursors.[Default]
              MessageBox.Show("TV series not found! Please add the year in the name!")
              Exit Sub
          End If
      End If
      Dim LinkFinal As String = (GetLink(ImdbURL))
      If String.IsNullOrEmpty(LinkFinal) Or String.IsNullOrWhiteSpace(LinkFinal) Then
          Cursor.Current = Cursors.[Default]
          Exit Sub
      End If
      Clipboard.SetText(LinkFinal)
      MessageBox.Show("Link in clipboard. Paste it into desired browser!")
      TextFromBox = String.Empty
      StatusLink = "0"
      Cursor.Current = Cursors.[Default]
  End Sub
  
  Public Function HaveInternetConnection() As Boolean
      Dim result As Boolean
      Try
          result = My.Computer.Network.Ping("www.google.com")
      Catch ex As Exception
          result = False
      End Try
      Return result
  End Function
  
  Private Function GetIP() As String
      Return Client.DownloadString("https://api.ipify.org").ToString()
  End Function
  
  Private Function matchAll(ByVal regex As String, ByVal html As String, ByVal Optional i As Integer = 1) As ArrayList
      Dim list As ArrayList = New ArrayList()
  
      For Each m As Match In New Regex(regex, RegexOptions.Multiline).Matches(html)
          list.Add(m.Groups(i).Value.Trim())
      Next
  
      Return list
  End Function
  
  Private Function getIMDbUrl(MovieName As String, Optional searchEngine As String = "google") As String
      Dim address As String = GoogleSearch + MovieName
      If searchEngine.ToLower().Equals("bing") Then
          address = BingSearch + MovieName
      End If
      If searchEngine.ToLower().Equals("ask") Then
          address = AskSearch + MovieName
      End If
      Dim html As String = Client.DownloadString(address)
      Dim arrayList As ArrayList = matchAll("&lt;a href=""(*****\d{7}/)"".*?&gt;.*?&lt;/a&gt;", html, 1)
      If arrayList.Count &gt; 0 Then
          Return CStr(arrayList(0))
      ElseIf searchEngine.ToLower().Equals("google") Then
          Return getIMDbUrl(MovieName, "bing")
      ElseIf searchEngine.ToLower().Equals("bing") Then
          Return getIMDbUrl(MovieName, "ask")
      Else
          Return String.Empty
      End If
  End Function
  
  Private Function GetLink(ImdbURL As String) As Object
      Dim ID = ImdbURL.Replace("*****/", "")
      ID = ID.Replace("/", "")
      Dim result As Object
      If StatusLink = "1" Then
          Cursor.Current = Cursors.[Default]
          Exit Function
      ElseIf Not Button4.Enabled Then
          Dim SeasonNumber As String = "&amp;s=" + RichTextBox2.Text
          Dim EpisodeNumber As String = "&amp;e=" + RichTextBox3.Text
          Dim TVSeasonNumber As String = "&amp;tv=1" + SeasonNumber
          If Not Client.DownloadString("*****/" &amp; ID).Contains("&gt;Episode Guide&lt;") Then
              ImdbURL = getIMDbUrl(TextFromBox + " TV series")
              StatusLink = "1"
              If String.IsNullOrEmpty(ImdbURL) Or String.IsNullOrWhiteSpace(ImdbURL) Then
                  Cursor.Current = Cursors.[Default]
                  MessageBox.Show("TV series not found! Please add the year in the name or check category!")
                  result = String.Empty
                  Exit Function
              Else
                  ID = ImdbURL.Replace("*****", "")
                  ID = ID.Replace("/", "")
                  result = (GetLink(ImdbURL))
                  If String.IsNullOrEmpty(result) Or String.IsNullOrWhiteSpace(result) Then
                      Cursor.Current = Cursors.[Default]
                      MessageBox.Show("TV series not found! Please add the year in the name or check category!")
                      result = String.Empty
                      Exit Function
                  Else
                      Return result.ToString()
                      result = String.Empty
                      Exit Function
                  End If
              End If
          Else
              Dim address As String = "******" &amp; ID &amp; TVSeasonNumber &amp; "&amp;ip=" &amp; GetIP()
              Dim token As String = Client.DownloadString(address)
              result = "*****" &amp; ID &amp; TVSeasonNumber &amp; EpisodeNumber &amp; "&amp;ticket=" &amp; token
              Return result.ToString
              Exit Function
          End If
      ElseIf Not Button3.Enabled Then
          If Client.DownloadString("****" &amp; ID).Contains("&gt;Episode Guide&lt;") Then
              ImdbURL = getIMDbUrl(TextFromBox + " movie",)
              StatusLink = "1"
              If String.IsNullOrEmpty(ImdbURL) Or String.IsNullOrWhiteSpace(ImdbURL) Then
                  Cursor.Current = Cursors.[Default]
                  MessageBox.Show("Movie not found! Please add the year in the name or check category!")
                  result = String.Empty
                  Exit Function
              Else
                  ID = ImdbURL.Replace("****", "")
                  ID = ID.Replace("/", "")
                  result = (GetLink(ImdbURL))
                  If String.IsNullOrEmpty(result) Or String.IsNullOrWhiteSpace(result) Then
                      Cursor.Current = Cursors.[Default]
                      MessageBox.Show("Movie not found! Please add the year in the name or check category!")
                      result = String.Empty
                      Exit Function
                  Else
                      Return result.ToString()
                      result = String.Empty
                      Exit Function
                  End If
              End If
          Else
              Dim address2 As String = "****" &amp; ID &amp; "&amp;ip=" &amp; GetIP()
              Dim token2 As String = Client.DownloadString(address2)
              result = "****" &amp; ID &amp; "&amp;ticket=" &amp; token2
              Return result.ToString
              result = String.Empty
              Exit Function
          End If
      End If
  #Disable Warning BC42105 ' Function doesn't return a value on all code paths
  End Function
  
  Private Sub RichTextBox1_MouseClick(sender As Object, e As EventArgs) Handles RichTextBox1.GotFocus
      If RichTextBox1.Text.Contains("Type movie or tv series name here") Then RichTextBox1.Text = String.Empty
  End Sub
  
  Private Sub RichTextBox2_MouseClick(sender As Object, e As EventArgs) Handles RichTextBox2.GotFocus
      If RichTextBox2.Text.Contains("S") Then RichTextBox2.Text = String.Empty
  End Sub
  
  Private Sub RichTextBox3_MouseClick(sender As Object, e As EventArgs) Handles RichTextBox3.GotFocus
      If RichTextBox3.Text.Contains("E") Then RichTextBox3.Text = String.Empty
  End Sub
  
  Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
      Button3.Enabled = False
      Button4.Enabled = True
      RichTextBox1.Enabled = True
      RichTextBox2.Enabled = False
      RichTextBox3.Enabled = False
  End Sub
  
  Private Sub Button4_Click(sender As Object, e As EventArgs) Handles Button4.Click
      Button4.Enabled = False
      Button3.Enabled = True
      RichTextBox1.Enabled = True
      RichTextBox2.Enabled = True
      RichTextBox3.Enabled = True
  End Sub
  
  Private Sub RichTextBox2_KeyPress(sender As Object, e As KeyPressEventArgs) Handles RichTextBox2.KeyPress
      If Char.IsLetter(e.KeyChar) Then
          e.Handled = True
      End If
      If CType(e, KeyPressEventArgs).KeyChar = vbCr Then
          Search()
      End If
  End Sub
  
  Private Sub RichTextBox3_KeyPress(sender As Object, e As KeyPressEventArgs) Handles RichTextBox3.KeyPress
      If Char.IsLetter(e.KeyChar) Then
          e.Handled = True
      End If
      If CType(e, KeyPressEventArgs).KeyChar = vbCr Then
          Search()
      End If
  End Sub
  
  Private Sub RichTextBox1_KeyPress(sender As Object, e As KeyPressEventArgs) Handles RichTextBox1.KeyPress
      If CType(e, KeyPressEventArgs).KeyChar = vbCr Then
          Search()
      End If
      If Char.IsUpper(e.KeyChar) Then e.KeyChar = Char.ToLower(e.KeyChar)
  End Sub
  
  Private Sub RichTextBox1_Lostfocus(sender As Object, e As EventArgs) Handles RichTextBox1.LostFocus
      If String.IsNullOrEmpty(RichTextBox1.Text) Or String.IsNullOrWhiteSpace(RichTextBox1.Text) Then
          RichTextBox1.Text = "Type movie or tv series name here..."
      End If
  End Sub
  
  Private Sub RichTextBox2_Lostfocus(sender As Object, e As EventArgs) Handles RichTextBox2.LostFocus
      If String.IsNullOrEmpty(RichTextBox2.Text) Or String.IsNullOrWhiteSpace(RichTextBox2.Text) Then
          RichTextBox2.Text = "S:"
      End If
  End Sub
  
  Private Sub RichTextBox3_Lostfocus(sender As Object, e As EventArgs) Handles RichTextBox3.LostFocus
      If String.IsNullOrEmpty(RichTextBox3.Text) Or String.IsNullOrWhiteSpace(RichTextBox3.Text) Then
          RichTextBox3.Text = "E:"
      End If
  End Sub
  
  Private Sub RichTextBox1_TextChanged(sender As Object, e As EventArgs) Handles RichTextBox1.TextChanged
      StatusLink = "0"
  End Sub
  
  Private Sub RichTextBox2_TextChanged(sender As Object, e As EventArgs) Handles RichTextBox2.TextChanged
      StatusLink = "0"
  End Sub
  
  Private Sub RichTextBox3_TextChanged(sender As Object, e As EventArgs) Handles RichTextBox3.TextChanged
      StatusLink = "0"
  End Sub
  End Class
  </code></pre>
  
  <p>all of the ***** are https website addresses.
  Tried switching the Net version to 4.7. Did not help. It is also legal in my country to stream (interior minister dropped a bomb on the DMCA here lol) and this is streaming only. The code is not operational so i had not given you anything dangerous if it is illegal in yours.</p>