在我的 Wix 网站上使用 API 接收时发生意外错误

问题描述

我使用向我的 Wix 站点 API 发送请求创建了一个实验性的电子邮件和密码系统,并且我已经通过 curl 和邮递员验证了它的工作原理。但是,当尝试在我的 VB.NET 应用程序中使用它时,出现错误:“底层连接已关闭:接收时发生意外错误。”我之前已经遇到过“无法创建 SSL/TLS 安全通道”错误,但是当我将项目的 .NET 框架移至 4.7.1 并确保 SecurityProtocol 设置为 SystemDefault 时解决了该错误,因为 Wix 似乎使用 TLS 1.3 .但是现在我在接收时收到此错误,我不知道如何解决,因为请求中的所有内容似乎都没有问题:

ServicePointManager.SecurityProtocol = SecurityProtocolType.SystemDefault
Dim req As HttpWebRequest = DirectCast(WebRequest.Create(New Uri("https://xxxxx.com/mysite/_functions/check?email=" & email & "&pass=" & LCase(passHash))),HttpWebRequest)
req.Method = "POST"
req.ContentType = "none"
req.ContentLength = 0
req.Host = "test"
req.KeepAlive = True

Dim result As HttpWebResponse = req.GetResponse()

运行'req.GetReponse()'时抛出错误。 在查看了无数版本的 .NET POST 请求示例代码和有关此错误的问题后,但每个人似乎都建议设置安全协议类型,我已经完成并将其设置为系统认值是我没有得到的唯一方法内部服务器错误状态。另一个是将 KeepAlive 设置为 false,我也尝试过但没有任何区别。

这里可能有什么问题?如果有帮助,以下是邮递员标题中包含的内容

enter image description here

解决方法

好的,所以最终我最终将代码更改为使用 GetResponseAsync() 而不是 GetResponse()(并不是说它对问题有任何影响)并将协议类型设置回 .NET 4.8 上的 Tls12。最后,似乎设置 req.Host 是导致响应问题的原因,Wix 似乎不喜欢它。我还在请求中添加了一些额外的设置,但最终它们对响应没有影响。这是最终的工作代码:

ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12
Dim req As HttpWebRequest = WebRequest.CreateHttp("https://example.com/check?email=" & email & "&pass=" & LCase(passHash))
req.Method = "POST"
req.ContentType = "none"
req.ContentLength = 0
req.KeepAlive = True
req.CookieContainer = New CookieContainer()
req.UserAgent = "Test"
req.Accept = "ext/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8"
req.Headers.Add(HttpRequestHeader.AcceptLanguage,"en-US;q=0.9,en;q=0.5")
req.Headers.Add(HttpRequestHeader.AcceptEncoding,"gzip,deflate;q=0.8")
req.Headers.Add(HttpRequestHeader.CacheControl,"no-cache")

Dim statusCode As HttpStatusCode = Nothing
Try
    Using result As HttpWebResponse = CType(Await req.GetResponseAsync(),HttpWebResponse)
        statusCode = result.StatusCode
    End Using

    If statusCode = HttpStatusCode.OK Then
        MsgBox("Valid")
    Else
        Dim response As String = New StreamReader(req.GetResponse().GetResponseStream()).ReadToEnd()

        MsgBox(response)
    End If
Catch webEx As WebException
    MsgBox(New StreamReader(webEx.Response.GetResponseStream()).ReadToEnd())
End Try

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...