问题
我在日志中看到了以下异常信息:
The underlying connection was closed: An unexpected error occurred on a send.
这个错误会在任意时间(1-4小时)内突然出现,导致我们与电子邮件营销系统的 OEM 集成失败。
我的网站托管在 Windows Server 2008 R2 上,使用的是 IIS 7.5.7600。
该网站包含大量的 OEM 组件和全面的仪表盘。除了我们在仪表板中作为 iframe 解决方案使用的一个电子邮件营销组件外,其他所有元素都能正常工作。
它的工作方式是,我发送一个 HttpWebRequest 对象,其中包含所有凭据,然后我会得到一条 URL,我将其放入一个 iframe 中并进行操作。
但是,它只能运行一段时间(1-4小时),然后从
调用开始就出现上述异常。
即使系统试图从 httpWebRequest 获取 URL,也会出现同样的异常。
唯一使它再次工作的方法是:
- 重新启动应用程序池
- 在 web.config 中进行任何编辑。
我已经尝试了所有我能想到的选项,但都没有成功。
尝试的选项
明确添加了以下选项:
keep-alive = false
keep-alive = true
增加了超时时间:
<httpRuntime maxRequestLength="2097151" executionTimeout="9999999" enable="true" requestValidationMode="2.0" />
我已将此页面上传到一个非 SSL 网站上,以检查我们生产服务器上的 SSL 证书是否会导致连接有所下降。
非常感谢任何解决方案建议!
代码
Public Function CreateHttpRequestJson(ByVal url) As String
Try
Dim result As String = String.Empty
Dim httpWebRequest = DirectCast(WebRequest.Create("https://api.xxxxxxxxxxx.com/api/v3/externalsession.json"), HttpWebRequest)
httpWebRequest.ContentType = "text/json"
httpWebRequest.Method = "PUT"
httpWebRequest.ContentType = "application/x-www-form-urlencoded"
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 = "None", _
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, "")
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]() {":"})(2)
result = "https:" & result.Substring(0, result.Length - 2)
End Using
End Using
Me.midFrame.Attributes("src") = result
Catch ex As Exception
objLog.WriteLog("Error:" & ex.Message)
If (ex.Message.ToString().Contains("Invalid Email")) Then
'TODO Show message on UI
ElseIf (ex.Message.ToString().Contains("Email Taken")) Then
'TODO Show message on UI
ElseIf (ex.Message.ToString().Contains("Invalid Access Level")) Then
'TODO Show message on UI
ElseIf (ex.Message.ToString().Contains("Unsafe Password")) Then
'TODO Show message on UI
ElseIf (ex.Message.ToString().Contains("Invalid Password")) Then
'TODO Show message on UI
ElseIf (ex.Message.ToString().Contains("Empty Person Name")) 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) & ":" & Convert.ToString(userPassword)
authInfo = Convert.ToBase64String(Encoding.[Default].GetBytes(authInfo))
request.Headers("Authorization") = "Basic " & authInfo
End Sub