使用VBA调用WCF Web服务,如何更改绑定?

3

我正在开发一个Access 2010的“工具”,希望能够将我们本地的Access数据库与在线数据库同步(在线数据库托管在我们无法控制或直接访问的服务器上)。我的背景不是VBA或VB.net,所以我对这种语言还比较陌生,因此任何帮助或建议都将不胜感激。到目前为止,以下是我编写的测试代码,以验证连接能力并更好地了解在获取有效响应时返回的内容。

Public Sub SendXML()
    Dim myHTTP As MSXML2.ServerXMLHTTP60
    Dim myDom As MSXML2.DOMDocument
    Dim myXML As String
    Set myHTTP = CreateObject("MSXML2.ServerXMLHTTP.6.0")
    Set myDom = CreateObject("MSXML2.DOMDocument")
    myDom.async = False
    myXML = "<s:Envelope xmlns:a=" & Chr(34) & "http://www.w3.org/2005/08/addressing" & Chr(34) & "xmlns:s=" & Chr(34) & "http://www.w3.org/2003/05/soap-envelope" & Chr(34) & "><s:Header><a:Action s:mustUnderstand=" & Chr(34) & "1" & Chr(34) & ">http://tempuri.org/IWcfService/Get_InitiativeList_CSV</a:Action></s:Header><s:Body><Get_InitiativeList_CSV xmlns=" & Chr(34) & "http://tempuri.org/" & Chr(34) & "><userID>MYUSERID</userID></Get_InitiativeList_CSV></s:Body></s:Envelope>"
    myDom.LoadXML (myXML)
    myHTTP.Open "post", "https://server/WcfService/WcfService.svc", False
    myHTTP.setRequestHeader "Content-Type", "application/xml"   
    'myHTTP.send (myDom.XML)
    myHTTP.send (myXML)
    MsgBox myHTTP.responseText
End Sub

我回来了:

The server cannot service the request because the media type is unsupported.

需要翻译的内容如下:

A couple things to note above. I tried two different way to create the XML string. One creating it as you see above and sending that raw text to the server. And creating the string as you see above and using loadXML to create the string. I do not know why one would be better than the other but both returned the same error.

现在需要注意的是,我尝试过两种不同的方法来创建XML字符串。一种是创建以上所示的原始文本并将其发送到服务器,另一种是创建以上所示的字符串并使用loadXML来创建该字符串。我不知道哪种方法更好,但两种方法都返回了相同的错误。

现在,我已经尝试了SOA Cleaner Express,并成功连接到了服务并获取了数据。既然SOA Cleaner能够连接,我认为使用SOA在VBA中发送的原始SOAP/XML字符串可能是一个很好的起点。我注意到,使用SOA Cleaner时它具有WsHttpBinding作为WCF绑定,如果我将此绑定更改为BasicHttpBinding,我会得到与在VBA中遇到的类似的错误消息,确切地说是:

Content Type text/xml; charset=utf-8 was not supported by service 

我甚至不确定自己是否走在正确的方向上,但如果是的话,我该如何在VBA中设置或更改“绑定”?这里还有其他事情吗?我相信这很简单,但就像我说的,我没有VBA背景,WCF和SOAP对我来说也比较新。

我感激任何帮助。

2个回答

1

首先,客户端的绑定必须与服务相匹配,因此如果服务仅支持wsHttpBinding,则客户端也必须使用该绑定。

为了从VBA调用WCF服务并增加灵活性,包括指定绑定的能力,您可能希望使用moniker。以下是构建和在VBA中使用服务moniker的示例:

Dim addr As String
addr = "service:mexAddress=""net.tcp://localhost:7891/Test/WcfService1/Service1/Mex"","
addr = addr + "address=""net.tcp://localhost:7891/Test/WcfService1/Service1/"","
addr = addr + "contract=""IService1"", contractNamespace=""http://tempuri.org/"","
addr = addr + "binding=""NetTcpBinding_IService1"", bindingNamespace=""http://tempuri.org/"""

Dim service1 As Object
Set service1 = GetObject(addr)

MsgBox service1.GetData(12)

以下是一些包含此代码片段来源的更多信息链接:

http://msdn.microsoft.com/en-us/library/ms752245(v=vs.110).aspx http://damianblog.com/2009/07/05/excel-wcf/


谢谢您的回复和提供的链接。我已经访问了它们,但很快就迷失了方向。我需要更多的信息来开始。首先,从快速搜索来看,我不认为我要使用的服务有“MexAddress”,我也不知道或者理解什么是MexAddress。如果能帮助的话,我向服务发送的包是以SOAP格式排列的。 - wblakenc
实际上,SOAP是WCF的默认协议。您能否获取服务的WSDL来描述它?(在浏览器中将“?WSDL”添加到服务URL的末尾)。您如何知道服务上有哪些操作可用? - The other other Alan
是的,我可以通过浏览器获取WSDL。我还使用了SOA Cleaner Pro来加载WSDL并获取特定操作。我还有一份技术文档,描述了这些操作(期望输入和返回值)。 - wblakenc

0
这可能有些牵强,但考虑到第二个错误比第一个错误更具描述性,您是否尝试指定字符集?
myHTTP.setRequestHeader "Content-Type", "application/xml; charset=utf-8"

谢谢,但不幸的是添加 charset=utf-8 会导致相同的错误信息。 - wblakenc

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接