我正在尝试访问以下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 & "×tamp=" & 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
答案 0 :(得分:1)
以下链接解决了我的问题。
Http post error: An existing connection was forcibly closed by the remote host
我做了这个改变。
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;