WCF服务配置成wsHttpBinding+certificate证书加密+自定义的用户名密码检验功能。在服务的访问上用了https协议,个人也没做过java平台和.Net Web Service的整合。而且https无疑增加了这个难度。
所以刚开始的想法是通过asp.net网页来访问服务,再利用网页来给java,android返回xml。这样利用http避免了平台的不兼容性。
缺点是各种业务的增加删除不紧要在Web Service中增加,还要在网页中增加。
而后来由于另外几个同学迟迟没有开始做应用端,导致业务无法预料,时间越脱越长。
个人觉得时间可能有点紧,就想把框架简单化,直接让java端和andriod客户端调用WCF服务。
于是清明放假这几天改造了一下程序框架,网络上参考了各种文章,终于调试成功了。
废话不说了,看代码。
首先,最简单的服务,没有certificate,没有自定义的用户名密码检验,只有wsHttpBinding。
WCF服务配置程序如下:
<? xml version = "1.0" encoding = "utf-8" ?> < configuration > < system.serviceModel > < behaviors > < serviceBehaviors > < behavior name = "httpsBasicBindings" > < serviceMetadata httpGetEnabled = "false" httpGetUrl = "http://127.0.0.1:8889/test" /> < serviceDebug includeExceptionDetailInFaults = "true" /> </ behavior > </ serviceBehaviors > </ behaviors > < services > < service behaviorConfiguration = "httpsBasicBindings" name = "TestService.TestService" > < endpoint address = "" binding = "basicHttpBinding" bindingConfiguration = "test1" contract = "TestService.ITestService" > </ endpoint > < endpoint address = "mex" binding = "mexHttpsBinding" contract = "IMetadataExchange" /> </ service > </ services > < bindings > < basicHttpBinding > < binding name = "test1" > < security mode = "Transport" > < transport clientCredentialType = "None" /> </ security > </ binding > </ basicHttpBinding > </ bindings > </ system.serviceModel >
java客户端调用程序:(用axis2-1.4.1生成,生成方法cmd下 axis2的bin目录下 wsdl2java -uri http://localhost:8889/test?wsdl)
TestServiceStub.GetSysTime paraGetSysTime=new TestServiceStub.GetSysTime(); try { TestServiceStub.GetSysTimeResponse response = client.GetSysTime(paraGetSysTime); resultString=response.getGetSysTimeResult(); } catch (RemoteException e) { e.printStackTrace(); } } catch (AxisFault e) { e.printStackTrace(); } System.out.print(resultString);
结果调用失败,一直是什么read time out,超时。网查查阅资料,说这么设置就可以了。
client._getServiceClient().getOptions().setTimeOutInMilliSeconds( 5 * 60 * 1000 );
于是设置,结果还是超时。继续找资料。发现都是说超时。当时那个崩溃啊~~好在翻了10页google,终于找到一篇说是java不支持wshttpbinding协议。因为wshttpbinding是微软在SOAP1.0基础上改进过的,不是通用的绑定协议。这里又补充了下理论知识,呵呵~~于是改为basicHttpBinding。
WCF服务配置(basicHttpBinding)
<? xml version = "1.0" encoding = "utf-8" ?> < configuration > < system.serviceModel > < behaviors > < serviceBehaviors > < behavior name = "httpsBasicBindings" > < serviceMetadata httpGetEnabled = "true" httpGetUrl = "http://127.0.0.1:8889/test" /> < serviceDebug includeExceptionDetailInFaults = "true" /> </ behavior > </ serviceBehaviors > </ behaviors > < services > < service behaviorConfiguration = "httpsBasicBindings" name = "TestService.TestService" > < endpoint address = "" binding = "basicHttpBinding" bindingConfiguration = "test1" contract = "TestService.ITestService" > </ endpoint > < endpoint address = "mex" binding = "mexHttpsBinding" contract = "IMetadataExchange" /> </ service > </ services > < bindings > < basicHttpBinding > < binding name = "test1" > < security mode = "Transport" > < transport clientCredentialType = "None" /> </ security > </ binding > </ basicHttpBinding > </ bindings > </ system.serviceModel > </ configuration >
然后重新用axis2 生成客户端代理。调用代码,果然成功。调用代码不变。
调用结果图:
下面是axis2生成的客户端代理结构图。