有一对应用,A使用Java开发,B使用.NET开发;现要求B应用开发一个.NET WebService接口,将此接口URL登记到A应用中,当有消息产生时,A应用通过此接口向B应用报告。
WebService开发出来后联调发现,即使有消息产生,B应用接口也未能拿到任何数据,查看IIS日志却发现A应用是有在调用接口的。由于A系统已经是一个产品化的应用,所以只能是调整B应用去适应它。
首先抓包,发现接口调用IIS直接报告HTTP 500错误,错误信息如下:
System.InvalidOperationException: 请求格式无效: text/xml; charset=utf-8。
在 System.Web.Services.Protocols.HttpServerProtocol.ReadParameters()
在 System.Web.Services.Protocols.WebServiceHandler.CoreProcessRequest()
客户端抓包数据如下:
?xml version="1.0" encoding="UTF-8"?>
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"><soapenv:Body>
<ns1:orderRelationUpdateNotify soapenv:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" xmlns:ns1="http://soap.bossagent.vac.unicom.com">
<orderRelationUpdateNotifyRequest href="#id0"/></ns1:orderRelationUpdateNotify>
<multiRef soapenc:root="0" soapenv:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" xsi:type="ns2:OrderRelationUpdateNotifyRequest" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/" xmlns:ns2="http://req.sync.soap.bossagent.vac.unicom.com">
<recordSequenceId xsi:type="soapenc:string">20100701</recordSequenceId>
<userIdType href="#id1"/>
<userId xsi:type="soapenc:string">8618602392936</userId>
<serviceType xsi:type="soapenc:string">31</serviceType>
<spId xsi:type="soapenc:string">55255</spId>
<productId xsi:type="soapenc:string">2344101</productId>
<updateType href="#id2"/>
<updateTime xsi:type="soapenc:string">20100701120241</updateTime>
<updateDesc xsi:type="soapenc:string"></updateDesc>
<linkId xsi:type="soapenc:string"></linkId><content xsi:type="soapenc:string">qb</content>
<effectiveDate xsi:type="soapenc:string">20100701111700</effectiveDate>
<expireDate xsi:type="soapenc:string">20100701120237</expireDate>
<time_stamp xsi:type="soapenc:string">0701120241</time_stamp>
<encodeStr xsi:type="soapenc:string"></encodeStr>
</multiRef>
<multiRef soapenc:root="0" soapenv:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" xsi:type="soapenc:int" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/">2</multiRef>
<multiRef soapenc:root="0" soapenv:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" xsi:type="soapenc:int" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/">1</multiRef>
</soapenv:Body>
</soapenv:Envelope>
.net开发的web service如下:
[WebService(Namespace = "http://temp.com/")]
[WebServiceBinding(ConformsTo = WsiProfiles.None)]
[SoapDocumentService(RoutingStyle = SoapServiceRoutingStyle.RequestElement)]
[System.ComponentModel.ToolboxItem(false)]
public class OrderRelationSync : System.Web.Services.WebService
{
[SoapRpcMethodAttribute(Action = "", Use = SoapBindingUse.Encoded)]
[WebMethod]
public OrderRelationUpdateNotifyResponse AcceptOrder(
string recordSequenceID,
int userIdType,
string userId,
string serviceType,
string spId,
string productId,
int updateType,
string updateTime,
string updateDesc,
string linkID,
string content,
string effectiveDate,
string expireDate,
string time_Stamp,
string encodeStr)
{
}
客户端的soap请求中userIdType变成了这种格式:<userIdType href="#id1"/>,服务端的userIdType变量值要从其后的id为id1的<multiRef>1<multiRef>中取到,但是由于请求端不能修改,我不知道该如何修改.net web service来适应客户端。请各位帮忙。分不够再加
更正:如果java端已经无法修改,那么大概我说的都是废话了,呵呵!
---------------------------------------------------------------------------------------------------
使用 metro 工具,将 .net 的 wcf 服务生成针对 java 平台的客户端调用类(java),这样你自己就不用维护分布式计算中间的信息交互过程了,包括编解码这样的琐碎事情。
具体的使用方法,可以通过搜索查找。总之,我有过这样的经历,其实.net 与java 交互很简单。搜索结果,比如:
http://chaizhixue2005.blog.sohu.com/139840107.html
To implement a Java client to communicate with the WCF server, you can follow the steps below:
1. Download METRO/WSIT from the home page of Metro: https://metro.dev.java.net.
2. Download Eclipse. We use Eclipse + Metro to develop Dynamsoft SCM Anywhere.
3. Install Metro by executing the command: java –jar metro-1_4.jar
. The installation folder of Metro contains some documents, tools and samples. You can find the documents in the “docs” folder.
4. Use the C# project “WcfService1″ (provided in my WCF client and WCF service article) as the WCF server. Go to Property of the WCF project, and set the server port to one that is not occupied by other services. Here we used 8888 for example. In the “web.config” file, change the string “wsHttpBinding” to “basicHttpBinding”.
5. This is the key step. We use the wsimport tool included in Metro to generate the Java client code. Create a file named “service1.xml” and copy the following code to the file:
<bindings
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/"
wsdlLocaption="http://localhost:8888/Service1.svc?wsdl"
xmlns="http://java.sun.com/xml/ns/jaxws">
<bindings node="wsdl:definitions">
<enableAsyncMapping>true</enableAsyncMapping>
</bindings>
</bindings>
The parameter “enableAsyncMapping” means generating the asynchronous method communicating to WCF server. Save this file, and execute the following command:
bin\wsimport -extension -keep -Xnocompile -b TestService.xml http://localhost:8888/Service1.svc?wsdl
Then you can find two new directories in Metro folder: “org” and “com”. They are the generated Java code.
6. Open Eclipse IDE, create a new Java project named “SimpleWCFClient”, and copy the two new directories “org” and “com” to the “src” folder of the project. Refresh the project, and you can find that some new code files are in the project.
7. Create a test class named “WCFTest” and write the following code to the file:
import java.net.URL;
import javax.xml.namespace.QName;
import javax.xml.ws.BindingProvider;
import org.tempuri.IService1;
import org.tempuri.Service1;
public class WCFTest {
public static void main(String[] strArgs)
{
try
{
Service1 service1 = new Service1(new URL("http://localhost:8888/Service1.svc?wsdl"), new QName("http://tempuri.org/", "Service1"));
IService1 port = service1.getBasicHttpBindingIService1();
((BindingProvider)port).getRequestContext().put(BindingProvider.ENDPOINT_ADDRESS_PROPERTY, "http://localhost:8888/Service1.svc");
int input = 100;
String strOutput = port.getData(input);
System.out.println(strOutput);
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
8. Four .jar files need to be added to the Java project. You can get these files in the “lib” folder of Metro: webservices-api.jar, webservices-extra.jar, webservices-extra-api.jar, webservices-rt.jar. Then go to Property of the project and add these jars to the project.
9. Compile and run the Java project. If the Eclipse console outputs “You entered: 100″, congratulations, you are successful.
看看 AXIS 能不能解决你的问题。
http://ws.apache.org/axis/java/developers-guide.html