title: HTTPS - Unable to read data from the transport connection: An existing connection was forcibly closed by the remote host
body: <p>I am trying to access the below API <code>Create</code> method. I'm getting the following  error when attempting to call <code>webRequest1.GetRequestStream()</code>:</p>
  
  <blockquote>
    <p>Unable to read data from the transport connection: An existing connection was forcibly closed by the remote host</p>
  </blockquote>
  
  <p>Code:</p>
  
  <pre><code>Public Function OfferAffirmRequestURL(ByVal strHost As String, ByVal strClientId As String, ByVal strSecretKey As String, ByVal strApiPath As String) As String
      Dim strOfferCommand As String = ""
      Dim strHROBUrl As String = ""
      Dim strHROBRequestURL As String = "https://" &amp; strHost
      Dim strErrorMessage As String
  
      Dim strXMLString As String = "&lt;?xml version=""1.0"" encoding=""UTF-8""?&gt;&lt;Record&gt;&lt;ReferenceID&gt;44&lt;/ReferenceID&gt;&lt;Template&gt;New Offer&lt;/Template&gt;&lt;RecordData&gt;&lt;FormField&gt;&lt;candidate_first_name&gt;RBFName16&lt;/candidate_first_name&gt;&lt;candidate_last_name&gt;LName160&lt;/candidate_last_name&gt;&lt;candidate_email&gt;rbinteg160@affirmsoftware.com.au&lt;/candidate_email&gt;&lt;candidate_mobile_no&gt;0411111111&lt;/candidate_mobile_no&gt;&lt;start_date&gt;2014-07-16T00:00:00&lt;/start_date&gt;&lt;/FormField&gt;&lt;/RecordData&gt;&lt;/Record&gt;"
      Dim strTimeStamp As String = DateTime.UtcNow.ToString("yyyy-MM-ddTHH:mm:ssZ")
      Dim data As String = "clientId" &amp; strClientId &amp; "payload" &amp; strXMLString &amp; "timestamp" &amp; strTimeStamp
  
      Dim key As Byte() = StringToByteArray(strSecretKey)
      Dim encrypt As HMACSHA1 = New HMACSHA1(key)
      Dim dataBytes As Byte() = Encoding.ASCII.GetBytes(data)
      Dim signatureBytes As Byte() = encrypt.ComputeHash(dataBytes)
      Dim strSignature As String = Convert.ToBase64String(signatureBytes)
      Dim strSignatureParam As String = Uri.EscapeDataString(strSignature)
  
      strHROBUrl = strHROBRequestURL &amp; strApiPath &amp; "?clientId=" &amp; strClientId &amp; "&amp;payload=" &amp; strXMLString &amp; "&amp;timestamp=" &amp; strTimeStamp &amp; "&amp;signature=" &amp; strSignatureParam
  
      Dim byteArray As Byte() = Encoding.UTF8.GetBytes(strXMLString)
      ServicePointManager.Expect100Continue = False
  
      Dim webRequest1 As WebRequest = WebRequest.Create(strHROBUrl)
      'Dim webRequest1 As HttpWebRequest = DirectCast(WebRequest.Create(strHROBUrl), HttpWebRequest)
      webRequest1.Method = "POST"
      webRequest1.ContentType = "text/xml"
      webRequest1.ContentLength = byteArray.Length
      webRequest1.Credentials = CredentialCache.DefaultCredentials
  
      Try
          Dim requestWriter As IO.Stream = webRequest1.GetRequestStream()
          requestWriter.Write(byteArray, 0, byteArray.Length)
          requestWriter.Close()
  
          Dim response As WebResponse
          response = webRequest1.GetResponse()
          Dim responseReader As New IO.StreamReader(response.GetResponseStream())
  
          Dim responseData As String = responseReader.ReadToEnd()
          strErrorMessage = responseData
          responseReader.Close()
          webRequest1.GetResponse().Close()
  
      Catch ex As WebException
          strErrorMessage = ex.InnerException.Message
  
      End Try
  
      Return strErrorMessage
  End Function
  </code></pre>
  
  <p>The above method gets called in the button <code>Click</code> method shown below:</p>
  
  <pre><code>Protected Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
      Dim strCreateOfferURL As String = ""
      strCreateOfferURL = OfferAffirmRequestURL("abc.xyz.com", "xyz", "4d8b7jpojpodf031e", "/api/record/createRecord.shtml")
      Label1.Text = strCreateOfferURL
  End Sub
  </code></pre>