vb.net HTTPS - 无法从传输连接读取数据:远程主机强行关闭现有连接

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/42299453/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 20:12:04  来源:igfitidea点击:

HTTPS - Unable to read data from the transport connection: An existing connection was forcibly closed by the remote host

vb.nethttps

提问by razor

I am trying to access the below API Createmethod. I'm getting the following error when attempting to call webRequest1.GetRequestStream():

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

Unable to read data from the transport connection: An existing connection was forcibly closed by the remote host

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

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://" & 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>[email protected]</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

The above method gets called in the button Clickmethod shown below:

上述方法在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

回答by razor

Below link solved my issue.

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

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

Http post 错误:一个现有的连接被远程主机强行关闭

I made this change.

我做了这个改变。

ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;

ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;