1

我在 vb.net/vs 2010 中使用了一些服务引用,但这是第一个需要证书和自定义肥皂头的服务引用。我获得了一个 WSDL 和请求 xml,我在soapUI 3.5 中成功测试了它。在 vb.net 中创建服务引用后,我无法弄清楚如何添加自定义肥皂头。

我看到了许多实现的示例IClientMessageInspector,但由于某种原因无法使它们中的任何一个工作。大多数与我对 Implements 关键字缺乏了解有关,但端点行为也存在问题。我已经为证书使用了一个端点行为,并且无法让两个端点行为在一个端点上工作。

我切换到 HttpWebRequest,但也无法让它工作。代码运行,但我从服务器收到类似的错误。错误是"Remote server returned an error:(500) Internal Server Error".我用Fiddler看实际的请求和响应,响应来自他们的数据电源服务器,"TID:247456978.Catastrophic Error. Please call IAS Datapower Support [ 'dp:reject' executed - Unable to find metadata for 'long_url' ]".开发者的响应是他们的系统运行正常,所以是我的问题。我同意,因为我可以使用soapUI 获得有效响应。

所以问题是,我做错了什么。下面的代码看起来是否正确。我是否走在正确的轨道上,还是应该放弃这种尝试并寻找另一种解决方案来在不使用服务参考的情况下发出肥皂请求。

   Public Shared Sub TryIt()
    Try
        Dim sSoapEnv As String = "<soap:Envelope>
           {Rest of soap envelope here}"
        sSoapEnv &= "</soap:Envelope>"
        'MsgBox(sSoapEnv)
        ' Create a request using a URL that can receive a post. 
        Dim request As HttpWebRequest = HttpWebRequest.Create("https://yada.yada.yada.com:443/restofurl")

        Dim cert As New X509Certificate2 
        cert.Import("T:\fullpathtocert\fw.hdnipa.com.p12", "cert_password", X509KeyStorageFlags.PersistKeySet)

        Dim cred As New NetworkCredential("gdebacke.fmcdmn.local", "domain_password")
        request.Credentials = cred 
        request.ClientCertificates.Add(cert)


        ' Set the Method property of the request to POST.
        request.Method = "POST"
        ' Create POST data and convert it to a byte array.
        Dim postData As String = sSoapEnv
        Dim byteArray As Byte() = Encoding.UTF8.GetBytes(postData)
        ' Set the ContentType property of the WebRequest.
        request.ContentType = "application/x-www-form-urlencoded"
        ' Set the ContentLength property of the WebRequest.
        request.ContentLength = byteArray.Length
        ' Get the request stream.
        Dim dataStream As Stream = request.GetRequestStream()
        ' Write the data to the request stream.

        dataStream.Write(byteArray, 0, byteArray.Length)
        ' Close the Stream object.
        dataStream.Close()
        ' Get the response.
        Dim response As HttpWebResponse = request.GetResponse()
        ' Display the status.                   
        Console.WriteLine(response.StatusDescription)
        ' Get the stream containing content returned by the server.
        dataStream = response.GetResponseStream()
        ' Open the stream using a StreamReader for easy access.
        Dim reader As New StreamReader(dataStream)
        ' Read the content.
        Dim responseFromServer As String = reader.ReadToEnd()
        ' Display the content.
        Console.WriteLine(responseFromServer)
        ' Clean up the streams.
        reader.Close()
        dataStream.Close()
        response.Close()
    Catch ex As Exception
        MsgBox(ex.Message)
    End Try

End Sub
4

0 回答 0