分basicHttpBinding和wsHttpBinding两种情况:
一、basicHttpBinding比较简单一点,先来看看它所要求的HTTP包:
POST /WCFService1/Service.svc HTTP/1.1
Content-Type: application/soap+xml; charset=utf-8
Host: 127.0.0.1:3673
Content-Length: 566
Expect: 100-continue
Connection: Keep-Alive
<s:Envelope xmlns:s="http://www.w3.org/2003/05/soap-envelope">
<s:Body>
<MyOperation2 xmlns="http://tempuri.org/">
<myValue1>3</myValue1>
</MyOperation2>
</s:Body></s:Envelope>
经过tcpmon-1.0查看AXIS2发的包,头里边有个chunk然后没有Content-Length,所以在程序中axis2需要关掉chunk开关:
_messageContext.setProperty(HTTPConstants.CHUNKED, "false");
把chunk关掉后,会自动加上Content-Length。另外,Expect 100-continue与Connection: Keep-Alive还没生成,但不影响AXIS2调用WCF了。
二、wsHttpBinding稍微复杂一点点
还是先来看看它所需要的包:
POST /WCFService1/Service.svc HTTP/1.1
Content-Type: application/soap+xml; charset=UTF-8;
Host: 127.0.0.1:3673
Content-Length: 642
<?xml version=‘1.0‘ encoding=‘UTF-8‘?>
<soapenv:Envelope xmlns:soapenv="http://www.w3.org/2003/05/soap-envelope">
<soapenv:Header xmlns:wsa="http://www.w3.org/2005/08/addressing">
<wsa:To>http://127.0.0.1:3673/WCFService1/Service.svc</wsa:To>
<wsa:ReplyTo>
<wsa:Address>http://www.w3.org/2005/08/addressing/anonymous</wsa:Address>
</wsa:ReplyTo>
<wsa:MessageID>urn:uuid:651A41AA122CE788291212918783422</wsa:MessageID>
<wsa:Action>http://tempuri.org/IMyService/MyOperation2</wsa:Action>
</soapenv:Header>
<soapenv:Body>
<ns1:MyOperation2 xmlns:ns1="http://tempuri.org/">
<ns1:myValue1>33</ns1:myValue1>
</ns1:MyOperation2>
</soapenv:Body>
</soapenv:Envelope>
它与basicHttpBinding有什么区别?区别在于多了Soapenv:Header部分,而且使用了WS-Addressing。所以要使AXIS2的WS-Addressing enable,怎么弄?网上找了很久,说是sample中有的,就去看sample:axis2-1.4\samples\userguide\src\userguide\clients\ClientSideModuleEngagement.java
原来要先读一个axis2.xml这样的配置文件
我在最长的构造函数中加入了这么一句:
public WSHttpBinding_IMyServiceStub(
org.apache.axis2.context.ConfigurationContext configurationContext,
java.lang.String targetEndpoint, boolean useSeparateListener)
{
if(configurationContext == null)
{
File repository = new File("D:\\Program Files\\axis2-1.4\\repository");
if (!repository.exists()) {
try {
throw new FileNotFoundException("Repository Doesnot Exist");
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
//copy the LoggingModule.mar to "modules" folder.
//then modify the axis2.xml that is generating there according to
//phases that being included in the "module.xml"
configurationContext = ConfigurationContextFactory.
createConfigurationContextFromFileSystem(repository.getAbsolutePath(),
"D:\\Program Files\\axis2-1.4\\conf\\axis2.xml");
}
...
}
一般装好的axis2.xml都会加入所有的modules,其中会包括addressing,所以是有效的。能不能最简自己再试试吧。
然后再加入以下几句:
_serviceClient.engageModule(new javax.xml.namespace.QName( org.apache.axis2.Constants.MODULE_ADDRESSING ) );//编译器说这个过时了,新的不知道怎么写,先将就着吧
_serviceClient.getOptions().setTo(new EndpointReference(http://127.0.0.1:3673/WCFService1/Service.svc)); //写上<soapenv:header>中<wsa:To>的地址
_serviceClient.getOptions().setProperty( AddressingConstants.INCLUDE_OPTIONAL_HEADERS, Boolean.TRUE); // 不加这句的话在<soapenv:header>只有<wsa:To> 和<wsa:Action>还有<wsa:MessageID>,其中MessageID是自动生成的一个唯一的编号
三、Mtom
经过上面的折磨,Mtom相对来说简单一点,只要在WCF服务器端打开Mtom,然后客户端在axis2.xml中有个<parameter name="enableMTOM">false</parameter>,改成true就好了。