title: "The underlying connection was closed: An unexpected error occurred on a send." With SSL Certificate
body: <h2>Issue</h2>
  <p>I get this exception</p>
  <blockquote>
  <p>The underlying connection was closed: An unexpected error occurred on a send.</p>
  </blockquote>
  <p>in my logs, and it is breaking our OEM integration with our e-mail marketing system at random times. <em>(varying from 1-4 hours)</em></p>
  <p>My website is hosted on a Windows Server 2008 R2 with IIS 7.5.7600.</p>
  <p>This website has a large number of OEM components, and comprehensive dashboard. Everything works fine with all the other elements of the website except with one of our e-mail marketing component which we are using as an iframe solution within our dashboard.</p>
  <p>The way it works is, I send a <strong><a href="https://docs.microsoft.com/en-us/dotnet/api/system.net.httpwebrequest?view=net-6.0" rel="nofollow noreferrer">HttpWebRequest</a></strong> object with all the credentials, and I get a url back which I put in an iframe and it works.</p>
  <p>But it only works for some time (1-4 hours), and then from the call to</p>
  <blockquote>
  <p><a href="https://docs.microsoft.com/en-us/dotnet/api/system.net.httpwebrequest.getresponse?view=net-6.0" rel="nofollow noreferrer"><strong>webRequest.GetResponse();</strong></a></p>
  </blockquote>
  <p>I get the exception</p>
  <blockquote>
  <p>The underlying connection was closed: An unexpected error occurred on a send.</p>
  </blockquote>
  <p>Even if the system tries to get the URL from the <strong>httpWebRequest</strong> it fails with the same exception.</p>
  <p>The only way to make it work again is:</p>
  <ul>
  <li>to recycle the application pool</li>
  <li>anything is edited in web.config.</li>
  </ul>
  <p>I am really exhausted all the option that i could think of.</p>
  <h2>Options tried</h2>
  <p>Explicitly added,</p>
  <ul>
  <li><p><code>keep-alive = false</code></p>
  </li>
  <li><p><code>keep-alive = true</code></p>
  </li>
  <li><p>Increased the time out:</p>
  <p><code>&lt;httpRuntime maxRequestLength=&quot;2097151&quot; executionTimeout=&quot;9999999&quot; enable=&quot;true&quot; requestValidationMode=&quot;2.0&quot; /&gt;</code></p>
  </li>
  </ul>
  <p>I have uploaded this page to a non SSL website to check if the SSL certificate on our production server is making the connection to drop some how.</p>
  <p>Any direction toward resolution is greatly appreciated.</p>
  <h2>Code</h2>
  <pre><code>
      Public Function CreateHttpRequestJson(ByVal url) As String
          Try
              Dim result As String = String.Empty
              Dim httpWebRequest = DirectCast(WebRequest.Create(&quot;https://api.xxxxxxxxxxx.com/api/v3/externalsession.json&quot;), HttpWebRequest)
              httpWebRequest.ContentType = &quot;text/json&quot;
              httpWebRequest.Method = &quot;PUT&quot;
              httpWebRequest.ContentType = &quot;application/x-www-form-urlencoded&quot;
              httpWebRequest.KeepAlive = False
              'ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3
  
              'TODO change the integratorID to the serviceproviders account Id, useremail 
              Using streamWriter = New StreamWriter(httpWebRequest.GetRequestStream())
                  Dim json As String = New JavaScriptSerializer().Serialize(New With { _
                  Key .Email = useremail, _
                  Key .Chrome = &quot;None&quot;, _
                  Key .Url = url, _
                  Key .IntegratorID = userIntegratorID, _
                  Key .ClientID = clientIdGlobal _
                  })
  
                  'TODO move it to the web.config, Following API Key is holonis accounts API Key
                  SetBasicAuthHeader(httpWebRequest, holonisApiKey, &quot;&quot;)
                  streamWriter.Write(json)
                  streamWriter.Flush()
                  streamWriter.Close()
  
                  Dim httpResponse = DirectCast(httpWebRequest.GetResponse(), HttpWebResponse)
                  Using streamReader = New StreamReader(httpResponse.GetResponseStream())
                      result = streamReader.ReadToEnd()
                      result = result.Split(New [Char]() {&quot;:&quot;})(2)
                      result = &quot;https:&quot; &amp; result.Substring(0, result.Length - 2)
                  End Using
              End Using
              Me.midFrame.Attributes(&quot;src&quot;) = result
          Catch ex As Exception
              objLog.WriteLog(&quot;Error:&quot; &amp; ex.Message)
              If (ex.Message.ToString().Contains(&quot;Invalid Email&quot;)) Then
                  'TODO Show message on UI
              ElseIf (ex.Message.ToString().Contains(&quot;Email Taken&quot;)) Then
                  'TODO Show message on UI
              ElseIf (ex.Message.ToString().Contains(&quot;Invalid Access Level&quot;)) Then
                  'TODO Show message on UI
              ElseIf (ex.Message.ToString().Contains(&quot;Unsafe Password&quot;)) Then
                  'TODO Show message on UI
              ElseIf (ex.Message.ToString().Contains(&quot;Invalid Password&quot;)) Then
                  'TODO Show message on UI
              ElseIf (ex.Message.ToString().Contains(&quot;Empty Person Name&quot;)) Then
                  'TODO Show message on UI
              End If
          End Try
      End Function
    
  
      Public Sub SetBasicAuthHeader(ByVal request As WebRequest, ByVal userName As [String], ByVal userPassword As [String])
          Dim authInfo As String = Convert.ToString(userName) &amp; &quot;:&quot; &amp; Convert.ToString(userPassword)
          authInfo = Convert.ToBase64String(Encoding.[Default].GetBytes(authInfo))
          request.Headers(&quot;Authorization&quot;) = &quot;Basic &quot; &amp; authInfo
      End Sub
  </code></pre>