HttpWebRequest是否正确制作肥皂请求?

时间:2012-06-19 20:31:14

标签: xml vb.net soap

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

我看到许多实现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

0 个答案:

没有答案