调用Java++web+service帮助文档.docx
- 文档编号:23325568
- 上传时间:2023-05-16
- 格式:DOCX
- 页数:12
- 大小:101.51KB
调用Java++web+service帮助文档.docx
《调用Java++web+service帮助文档.docx》由会员分享,可在线阅读,更多相关《调用Java++web+service帮助文档.docx(12页珍藏版)》请在冰豆网上搜索。
调用Java++web+service帮助文档
C++调用Javawebservice帮助文档
2008年4月
目录
目录1
一、简介3
二、使用说明3
2.1安装使用SoapToolk3
2.2使用mssoaptookit3.0监视C++调用webservice3
2.3不使用mssoaptookit3.0监视C++调用webservice9
2.4被调用的方法参数是数组的情形9
一、简介
在做java项目的时候,有时候我们需要提供一些方法给外部调用,调用者可以处在网络上的任何位置,只要通过特定的设置就可以调用java提供的方法。
在java中,我们可以通过把这些方法做成webservice。
本文档主要对用VisualC++调用java做的webservice进行一系列的说明
二、使用说明
C++调用webservice的实质是:
发送soap请求并得到返回的结果。
用C++调用java的webservice,发现与调用微软的webservice还是不同的,用其自带的例子无法实现与java做的webservice通讯,下面和大家分享一下实现的方法。
1.首先,需要安装微软的mssoaptookit3.0工具包,可以从微软的官方网站上下载;
2.使用java建立一个web服务 ,本例子的webservice使用开源项目cxf建立起来的,想了解cxf请登陆:
http:
//incubator.apache.org/cxf/
2.1安装使用SoapToolk
首先C++要调用webservice,首先要下载mssoaptookit3.0工具包,并安装好。
mssoaptookit必须是3.0,否则会出现错误。
默认安装好mssouptookit3.0後,就可以使用C++来调用webservice了。
这时候可以使用mssoaptookit3.0来监控C++的soap请求。
现在假设网络上有一个webservice:
http:
//192.168.0.10:
8082/ServiceGateway/ServiceServer,下面就这个webservice来详细描述使用mssoaptookit3.0来监控C++调用webservice的情形和不监控的情形,
2.2使用mssoaptookit3.0监视C++调用webservice
要想使用mssoaptookit3.0来监控,首先请打开TraceUtility。
打开时候请新建一个FormattedTrace,如下图
这时候会弹出一个如下图的窗口:
Localport,可以自己定义,使用本机上的一个空闲的端口即可,本例设置为:
8080,该端口的意义:
以后使用该端口来发送soap请求,Destination:
localhos中的localhost请设置为webservice的IP,本例应该设置为:
192.168.0.10,Destination:
80请设置为webservice的端口,本例设置为:
8082,设置好之后就可以通过localport来监控soap请求了。
先查看wsdl文件:
http:
//192.168.0.10:
8082/ServiceGateway/ServiceServer?
wsdl
xmlversion="1.0"encoding="utf-8"?
>
- definitionsxmlns: wsdl="http: //schemas.xmlsoap.org/wsdl/"xmlns: soap="http: //schemas.xmlsoap.org/wsdl/soap/"xmlns: tns="http: //gateway/"xmlns: xsd="http: //www.w3.org/2001/XMLSchema"name="IndexServiceService"targetNamespace="http: //gateway/"> - types> - schemaattributeFormDefault="unqualified"elementFormDefault="unqualified"targetNamespace="http: //gateway/"xmlns: tns="http: //gateway/"xmlns: xsd="http: //www.w3.org/2001/XMLSchema"> elementname="numList"type="tns: numList"/> - complexTypename="numList"> - sequence> elementname="arg0"type="xsd: int"/> elementmaxOccurs="unbounded"minOccurs="0"name="arg1"type="xsd: int"/> sequence> complexType> elementname="numListResponse"type="tns: numListResponse"/> - complexTypename="numListResponse"> - sequence> elementmaxOccurs="unbounded"minOccurs="0"name="return"type="xsd: int"/> sequence> complexType> elementname="login"type="tns: login"/> - complexTypename="login"> - sequence> elementminOccurs="0"name="arg0"type="xsd: string"/> sequence> complexType> elementname="loginResponse"type="tns: loginResponse"/> - complexTypename="loginResponse"> - sequence> elementminOccurs="0"name="return"type="xsd: string"/> sequence> complexType> schema> types> - messagename="loginResponse"> partelement="tns: loginResponse"name="parameters"/> message> - messagename="login"> partelement="tns: login"name="parameters"/> message> - messagename="numListResponse"> partelement="tns: numListResponse"name="parameters"/> message> - messagename="numList"> partelement="tns: numList"name="parameters"/> message> - portTypename="IndexService"> - operationname="numList"> inputmessage="tns: numList"name="numList"/> outputmessage="tns: numListResponse"name="numListResponse"/> operation> - operationname="login"> inputmessage="tns: login"name="login"/> outputmessage="tns: loginResponse"name="loginResponse"/> operation> portType> - bindingname="IndexServiceServiceSoapBinding"type="tns: IndexService"> bindingstyle="document"transport="http: //schemas.xmlsoap.org/soap/http"/> - operationname="numList"> operationsoapAction=""style="document"/> - inputname="numList"> bodyuse="literal"/> input> - outputname="numListResponse"> bodyuse="literal"/> output> operation> - operationname="login"> operationsoapAction=""style="document"/> - inputname="login"> bodyuse="literal"/> input> - outputname="loginResponse"> bodyuse="literal"/> output> operation> binding> - servicename="IndexServiceService"> - portbinding="tns: IndexServiceServiceSoapBinding"name="IndexServicePort"> addresslocation="http: //192.168.0.10: 8082/ServiceGateway/ServiceServer"/> port> service> definitions> 可见有两个远程方法: int[]numList(int,int[]) Stringlogin(String) 用vc建立一个console的程序,其全部代码如下: //testwebservice.cpp: Definestheentrypointfortheconsoleapplication. // #include"stdafx.h" #include #import"msxml4.dll" usingnamespaceMSXML2; #import"C: \ProgramFiles\CommonFiles\MSSoap\Binaries\mssoap30.dll"\ exclude("IStream","IErrorInfo","ISequentialStream","_LARGE_INTEGER",\ "_ULARGE_INTEGER","tagSTATSTG","_FILETIME") usingnamespaceMSSOAPLib30; voidAdd() { ISoapSerializerPtrSerializer; ISoapReaderPtrReader; ISoapConnectorPtrConnector; //Connecttotheservice. Connector.CreateInstance(__uuidof(HttpConnector30)); //使用EndPointURL属性指定Web服务,因为使用soaptookit3.0来监控,所以使用本地的8080端口来发送soap请求 Connector->Property["EndPointURL"]="http: //localhost: 8080/ServiceGateway/ServiceServer"; //这是不使用mssoaptookit3.0来监控的情形,需要直接发送请求到远端的端口。 //Connector->Property["EndPointURL"]="http: //192.168.0.10: 8082/ServiceGateway/ServiceServer"; Connector->Connect(); //开始消息 ////在与服务器连接后,我们需要指定Web服务完成的操作。 //为了指定该操作,我们需要再次使用SoapConnector的Property属性 Connector->Property["SoapAction"]="http: //localhost: 8080/ServiceGateway/ServiceServer"; //这是不使用mssoaptookit3.0来监控的情形 //Connector->Property["SoapAction"]="http: //192.168.0.10: 8082/ServiceGateway/ServiceServer"; Connector->BeginMessage(); //CreatetheSoapSerializerobject. Serializer.CreateInstance(__uuidof(SoapSerializer30)); //Connecttheserializerobjecttotheinputstreamoftheconnectorobject. Serializer->Init(_variant_t((IUnknown*)Connector->InputStream)); //创建SOAP消息 Serializer->StartEnvelope("","",""); Serializer->StartBody //开始SOAP消息中的一个元素,第一个参数描述了名字空间,是wsdl文件中的: //targetNamespace="http: //gateway/" //如果它是空值,就会缺省地使用SOAP-ENV。 第二、第三个参数 //调用login方法,参数名由wsdl文件决定: // complexTypename="login"> // sequence> // elementminOccurs="0"name="arg0"type="xsd: string"/> // sequence> // complexType> Serializer->StartElement("login","http: //server.hw/","STANDARD",""); Serializer->StartElement("arg0","","STANDARD",""); Serializer->WriteString("cellcom"); Serializer->EndElement(); Serializer->EndElement(); Serializer->EndBody(); Serializer->EndEnvelope(); //将该消息发送给web服务 Connector->EndMessage(); ////读取响应 . Reader.CreateInstance(__uuidof(SoapReader30)); //将reader联接到connector的输出字符串 Reader->Load(_variant_t((IUnknown*)Connector->OutputStream),""); //显示结果 printf("Answer: %s\n",(constchar*)Reader->RpcResult->text); return; } intmain(intargc,char*argv[]) { CoInitialize(NULL); Add(); CoUninitialize(); return0; } 上面的代码是一个完整的C++调用webservice的情形,运行之后会发现TraceUtility里有些信息,如下图: 第一个框里是一个soap请求,第二个框是soap请求返回的结果。 2.3不使用mssoaptookit3.0监视C++调用webservice 不使用mssoaptookit3.0监控的时候,C++的代码要做如下修改: //使用EndPointURL属性指定Web服务,因为使用soaptookit3.0来监控,所以使用本地的8080端口来发送soap请求 //Connector->Property["EndPointURL"]="http: //localhost: 8080/ServiceGateway/ServiceServer"; //这是不使用mssoaptookit3.0来监控的情形,需要直接发送请求到远端的端口。 Connector->Property["EndPointURL"]="http: //192.168.0.10: 8082/ServiceGateway/ServiceServer"; Connector->Connect(); //开始消息 ////在与服务器连接后,我们需要指定Web服务完成的操作。 //为了指定该操作,我们需要再次使用SoapConnector的Property属性 //Connector->Property["SoapAction"]="http: //localhost: 8080/ServiceGateway/ServiceServer"; //这是不使用mssoaptookit3.0来监控的情形 Connector->Property["SoapAction"]="http: //192.168.0.10: 8082/ServiceGateway/ServiceServer"; //要使用http: //192.168.0.10: 8082/ServiceGateway/ServiceServer来指定服务。 //其他的基本上没改变。 2.4被调用的方法参数是数组的情形 下面是调用numList的情形,通过查看wsdl知道,numList有两个参数: complexTypename="numList"> - sequence> elementname="arg0"type="xsd: int"/> elementmaxOccurs="unbounded"minOccurs="0"name="arg1"type="xsd: int"/> sequence> complexType> 通过询问webservice的开发人员知道,第一个参数是int,第二个参数是int[],该方法是把int[]里所有在第一参数之前的数加一, Serializer->StartEnvelope("","",""); Serializer->StartBody(""); //开始SOAP消息中的一个元素,第一个参数描述了名字空间, //如果它是空值,就会缺省地使用SOAP-ENV。 第二、第三个参数 Serializer->StartElement("numList","http: //gateway/","STANDARD",""); Serializer->StartElement("arg0","","STANDARD",""); Serializer->WriteString("2"); Serializer->EndElement(); Serializer->StartElement("arg1","","STANDARD",""); Serializer->WriteString("53"); Serializer->EndElement(); Serializer->StartElement("arg1","","STANDARD",""); Serializer->WriteString("58"); Serializer->EndElement(); 查看soap监控里的信息:
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 调用 Java web service 帮助 文档