【问题标题】:HTTPS - Unable to read data from the transport connection: An existing connection was forcibly closed by the remote hostHTTPS - 无法从传输连接读取数据:现有连接被远程主机强行关闭
【发布时间】:2017-02-17 13:40:41
【问题描述】:

我正在尝试访问以下 API Create 方法。尝试调用 webRequest1.GetRequestStream() 时出现以下错误:

无法从传输连接读取数据:现有连接被远程主机强行关闭

代码:

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://" & strHost
    Dim strErrorMessage As String

    Dim strXMLString As String = "<?xml version=""1.0"" encoding=""UTF-8""?><Record><ReferenceID>44</ReferenceID><Template>New Offer</Template><RecordData><FormField><candidate_first_name>RBFName16</candidate_first_name><candidate_last_name>LName160</candidate_last_name><candidate_email>rbinteg160@affirmsoftware.com.au</candidate_email><candidate_mobile_no>0411111111</candidate_mobile_no><start_date>2014-07-16T00:00:00</start_date></FormField></RecordData></Record>"
    Dim strTimeStamp As String = DateTime.UtcNow.ToString("yyyy-MM-ddTHH:mm:ssZ")
    Dim data As String = "clientId" & strClientId & "payload" & strXMLString & "timestamp" & 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 & strApiPath & "?clientId=" & strClientId & "&payload=" & strXMLString & "&timestamp=" & strTimeStamp & "&signature=" & 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

上面的方法在按钮Click方法中被调用,如下所示:

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

【问题讨论】:

  • Debug.Print URL...正确吗?该站点是否具有来自受信任 CA 的有效 SSL 证书或您需要在计算机上安装的自签名证书?
  • 您可能不希望 URL 中的 XML:"&amp;payload=" &amp; strXMLString
  • 您好 MrGadget 非常感谢您的回复。下面的链接解决了我的问题。

标签: vb.net https


【解决方案1】:

以下链接解决了我的问题。

Http post error: An existing connection was forcibly closed by the remote host

我做了这个改变。

ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;

【讨论】:

    猜你喜欢
    • 2011-07-22
    • 1970-01-01
    • 2014-11-19
    • 2018-02-15
    • 2015-01-14
    相关资源
    最近更新 更多