gpt4 book ai didi

java - 使用 VB.NET 4.5.1 客户端连接到基于 Java 的 Web 服务时出现解码错误

转载 作者:行者123 更新时间:2023-11-30 08:52:22 27 4
gpt4 key购买 nike

我正在尝试构建一个 Web 应用程序,它将从数据库读取数据/从数据库读取数据,查询数据并将数据发布到远程 Web 服务。 Web 应用程序是在 .NET Framework 4.5.1 上用 vb.net 编写的。我在 IDE 中使用 VS2013。

我添加了服务“https://service.provider.com/..../ws/soap?wsdl=SOAP.wsdl”作为服务引用,并编写了以下内容来查询该服务:

Dim client As Service1.SOAPClient = New Service1.SOAPClient("Service1Soap")

Dim session = New Service1.session With {.id = 0, .login = "username", .password = "password"}

Dim response = client.openSession(session)

If response.status = Service1.responseStatus.success Then
'yay
ElseIf response.status = Service1.responseStatus.failure Then
'boo
End If

web.config 中的 service.serviceModel 部分不是自动生成的,所以我不得不根据每次运行时收到的各种错误手动拼凑它。我最终想出了以下内容:

<system.serviceModel>
<bindings>
<basicHttpBinding>
<binding name="Service1Soap">
<security mode="Transport" />
</binding>
</basicHttpBinding>
<customBinding>
<binding name="Service1Soap12">
<textMessageEncoding messageVersion="Soap12" />
<httpTransport />
</binding>
</customBinding>
</bindings>
<client>
<endpoint address="https://service.provider.com/.../ws/soap?wsdl=SOAP.wsdl" binding="basicHttpBinding" bindingConfiguration="Service1Soap" contract="Service1.SOAP" name="Service1Soap" />
</client>
</system.serviceModel>

现在,当我运行该应用程序时,它会在尝试调用 openSession 时抛出错误并向我提供以下错误消息:

Unmarshalling Error: Namespace URIs and local names to the unmarshaller needs to be interned.  
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.ServiceModel.FaultException: Unmarshalling Error: Namespace URIs and local names to the unmarshaller needs to be interned.

Source Error:

Line 7771: <System.ComponentModel.EditorBrowsableAttribute(System.ComponentModel.EditorBrowsableState.Advanced)> _
Line 7772: Function Service1_SOAP_openSession(ByVal request As Service1.openSession) As Service1.openSessionResponse Implements Service1.SOAP.openSession
Line 7773: Return MyBase.Channel.openSession(request)
Line 7774: End Function
Line 7775:


Source File: c:\...\Visual Studio 2013\Projects\WebApplication1\WebApplication1\Service References\Service1\Reference.vb Line: 7773

Stack Trace:



[FaultException: Unmarshalling Error: Namespace URIs and local names to the unmarshaller needs to be interned. ]
System.Runtime.Remoting.Proxies.RealProxy.HandleReturnMessage(IMessage reqMsg, IMessage retMsg) +10733331
System.Runtime.Remoting.Proxies.RealProxy.PrivateInvoke(MessageData& msgData, Int32 type) +336
WebApplication1.Service1.SOAP.openSession(openSession request) +0
WebApplication1.Service1.SOAPClient.Service1_SOAP_openSession(openSession request) in c:\...\Projects\WebApplication1\WebApplication1\Service References\Service1\Reference.vb:7773
WebApplication1.Service1.SOAPClient.openSession(OpenSession openSession1) in c:\...\Visual Studio 2013\Projects\WebApplication1\WebApplication1\Service References\Service1\Reference.vb:7779

...

我已经使用 SoapUI 查询了网络服务,并将其捕获为来自网络服务的响应:

<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
<ns1:openSessionResponse xmlns:ns1="http://autogenerated.service.provider.com/">
<return xmlns:ns2="http://autogenerated.service.provider.com/">
<id>0</id>
<status>success</status>
<sessionId>8a9b868f4d4650ee014d488308e90243</sessionId>
</return>
</ns1:openSessionResponse>
</soap:Body>
</soap:Envelope>

根据第三方网络服务提供商的说法,此错误与基于 Java 的服务以及我使用基于 vb.net 的客户端有关。我显然需要编写一个 WCF 扩展来修改客户端和服务器之间的 SOAP 请求和响应,以删除 SOAP 信封中的 namespace 。这是我第一次做.net client-to-java webservice 项目,所以我不知道从哪里开始,服务提供商也无法提供任何更具体的帮助。

有人知道怎么做吗?

更新:我注意到我的服务 url 是错误的(它引用了导入的部分)。更正 url 后,web.config 正确生成,现在内容如下,但我仍然收到与上面相同的解码错误。

  <system.serviceModel>
<bindings>
<basicHttpBinding>
<binding name="SOAPSoapBinding">
<security mode="Transport" />
</binding>
</basicHttpBinding>
</bindings>
<client>
<endpoint address="https://service.provider.com/.../ws/soap"
binding="basicHttpBinding" bindingConfiguration="SOAPSoapBinding"
contract="ServiceReference1.SOAP" name="SOAPImplPort" />
</client>
</system.serviceModel>

更新 2: 使用 Fiddler2,我捕获了对网络服务的传出请求和返回值。然后,我使用 SoapUI 将 SoapUI 查询与 .net 生成的查询进行比较并编辑查询,直到获得有效响应。

.net 生成的查询:

<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
<s:Body xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<openSession xmlns="http://autogenerated.service.provider.com/">
<openSession xmlns="">
<id>0</id>
<login>username</login>
<password>password</password>
</openSession>
</openSession>
</s:Body>
</s:Envelope>

网络服务响应:

<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
<soap:Fault>
<faultcode>soap:Client</faultcode>
<faultstring>Unmarshalling Error: Namespace URIs and local names to the unmarshaller needs to be interned.</faultstring>
</soap:Fault>
</soap:Body>
</soap:Envelope>

在 soap ui 中编辑并运行 .net 查询:注意从 openSession 标记中删除 namespace 属性。

<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
<s:Body xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<openSession>
<openSession>
<id>0</id>
<login>username</login>
<password>password</password>
</openSession>
</openSession>
</s:Body>
</s:Envelope>

网络服务响应:

<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
<ns1:openSessionResponse xmlns:ns1="http://autogenerated.service.provider.com/">
<return xmlns:ns2="http://autogenerated.service.provider.com/">
<id>0</id>
<status>success</status>
<sessionId>8a9b868f4d55c42b014d56f00c990134</sessionId>
</return>
</ns1:openSessionResponse>
</soap:Body>
</soap:Envelope>

因此,现在的问题是:如何在发送到 Web 服务之前修改来自客户端应用程序的传出查询,以删除命名空间?

更新 3: 成功!通过编辑 Reference.vb 文件并将 WrapperNamespace 和 Namespace 属性设置为“”而不是“http://autogenerated.service.provider.com/”,我设法删除了命名空间。然后我将它们复制到同一命名空间内的分部类中,并在自动生成的代理类中注释掉自动生成的方法。如果将来重新生成代理类,那么它将给我一个构建错误,我只需要再次删除/注释掉它们。客户端现在可以毫无问题地调用网络服务。

CustomReference.vb:

Namespace ServiceReference1

<System.Diagnostics.DebuggerStepThroughAttribute(), _
System.CodeDom.Compiler.GeneratedCodeAttribute("System.ServiceModel", "4.0.0.0"), _
System.ComponentModel.EditorBrowsableAttribute(System.ComponentModel.EditorBrowsableState.Advanced), _
System.ServiceModel.MessageContractAttribute(WrapperName:="openSession", WrapperNamespace:="", IsWrapped:=True)> _
Partial Public Class openSession

<System.ServiceModel.MessageBodyMemberAttribute(Name:="openSession", [Namespace]:="", Order:=0), _
System.Xml.Serialization.XmlElementAttribute(Form:=System.Xml.Schema.XmlSchemaForm.Unqualified)> _
Public openSession1 As ServiceReference1.OpenSession

Public Sub New()
MyBase.New()
End Sub

Public Sub New(ByVal openSession1 As ServiceReference1.OpenSession)
MyBase.New()
Me.openSession1 = openSession1
End Sub
End Class
...
End Namespace

但是,有没有更好的方法呢?

最佳答案

我设法通过删除 namespace 、编辑 Reference.vb 文件并设置 WrapperNamespaceNamespace 属性来消除解码错误,关于我需要调用的特定方法,"" 而不是 "http://autogenerated.service.provider.com/"。然后我将它们复制到同一命名空间内的分部类中,并在自动生成的代理类中注释掉自动生成的方法。如果将来重新生成代理类,那么它将给我一个构建错误,我只需要再次删除/注释掉它们。客户端现在可以毫无问题地调用网络服务。

CustomReference.vb:

Namespace ServiceReference1

<System.Diagnostics.DebuggerStepThroughAttribute(), _
System.CodeDom.Compiler.GeneratedCodeAttribute("System.ServiceModel", "4.0.0.0"), _
System.ComponentModel.EditorBrowsableAttribute(System.ComponentModel.EditorBrowsableState.Advanced), _
System.ServiceModel.MessageContractAttribute(WrapperName:="openSession", WrapperNamespace:="", IsWrapped:=True)> _
Partial Public Class openSession

<System.ServiceModel.MessageBodyMemberAttribute(Name:="openSession", [Namespace]:="", Order:=0), _
System.Xml.Serialization.XmlElementAttribute(Form:=System.Xml.Schema.XmlSchemaForm.Unqualified)> _
Public openSession1 As ServiceReference1.OpenSession

Public Sub New()
MyBase.New()
End Sub

Public Sub New(ByVal openSession1 As ServiceReference1.OpenSession)
MyBase.New()
Me.openSession1 = openSession1
End Sub
End Class
...
End Namespace

关于java - 使用 VB.NET 4.5.1 客户端连接到基于 Java 的 Web 服务时出现解码错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30194035/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com