VB.NET WCF 使用 HTTP 和 HTTPS,检测到 302

VB.NET WCF using HTTP and HTTPS, detecting 302

我有一个可通过 HTTP 和 HTTPS 使用的 WCF 服务。 我们已经加入了 HTTP 支持以允许使用 WireShark 通过网络对 http://localhost/ 和 运行 进行本地测试以进行流量分析。 我们想通过 HTTPS 在野外部署代码。 我们有 URL 重写规则,这样如果有人选择 http://foo.example.org they are redirected via a 302 to https://foo.example.org,这就可以了。 过去,.NET 会对 302 感到不满并发送错误 - 现在它喜欢将其从您那里抽象出来并继续工作。这意味着当我们被重定向时我们现在遇到了一个问题,因为我们必须在使用 HTTP 时将绑定安全模式更改为 None,在使用 HTTPS 时更改为 Transport

是否有一种方法可以根据连接实际执行的操作来检测我们需要使用哪种安全模式,而不是我们目前采用的天真方法?

Public Sub DeterminineBindingSecurity(ByRef MyBinding As BasicHttpBinding, EndpointAddress As String)
    Select Case EndpointAddress.Split(":").First
        Case "https"
            MyBinding.Security.Mode = BasicHttpSecurityMode.Transport
            MyBinding.Security.Transport.ClientCredentialType = HttpClientCredentialType.None
        Case Else
            MyBinding.Security.Mode = BasicHttpSecurityMode.None
            MyBinding.Security.Transport.ClientCredentialType = HttpClientCredentialType.None
    End Select
End Sub

谢谢

我解决了。 在执行此操作之前,我使用 System.Net.Http.HttpClient 询问端点 URL 并更新 URL

URL = (Await (New Net.Http.HttpClient()).GetAsync(URL)).RequestMessage.RequestUri.ToString