CZoneSoft

Live simply to express my ambition (原淡蓝随笔) - CZoneSoft 致力于视频/虚拟现实的应用开发 承接软件开发

首页 新随笔 联系 订阅 管理
  136 Posts :: 8 Stories :: 1943 Comments :: 4 Trackbacks

下面是个控制台的样例
Toolkit3.0 终于给出VC6的样例了,1.0只能看到VB和ASP的

#include <stdio.h>

#import "msxml4.dll"
using namespace MSXML2;

#import "C:\Program Files\Common Files\MSSoap\Binaries\mssoap30.dll" \
            exclude("IStream", "IErrorInfo", "ISequentialStream", "_LARGE_INTEGER", \
                    "_ULARGE_INTEGER", "tagSTATSTG", "_FILETIME")
using namespace MSSOAPLib30;  //你机器得安装SOAP Toolkit3.0 ,1.0时,用using namespace时报错


void Add()
{
   ISoapSerializerPtr Serializer;
   ISoapReaderPtr Reader;
   ISoapConnectorPtr Connector;
   // Connect to the service.
   Connector.CreateInstance(__uuidof(HttpConnector30));  //HttpConnector30 失败,无法这样创建Connector,CXX0017 Error :Symbol “HttpConnector30“ not found(摇头、叹气!)
   Connector->Property["EndPointURL"] = "http://MyServer/Soap3DocSamples/DocSample1/Server/DocSample1.wsdl";  //这个当然得改成您自己的拉
   Connector->Connect();

   // Begin the message.
   //Connector->Property["SoapAction"] = "uri:AddNumbers";
   Connector->Property["SoapAction"] = "http://tempuri.org/DocSample1/action/Sample1.AddNumbers";
   Connector->BeginMessage();

   // Create the SoapSerializer object.
   Serializer.CreateInstance(__uuidof(SoapSerializer30));

   // Connect the serializer object to the input stream of the connector object.
   Serializer->Init(_variant_t((IUnknown*)Connector->InputStream));

   // Build the SOAP Message.
   Serializer->StartEnvelope("","","");
   Serializer->StartBody("");
   Serializer->StartElement("AddNumbers","http://tempuri.org/DocSample1/message/","","");  //这是本地的Web Services,实际中要指定命名空间
   Serializer->StartElement("NumberOne","","","");
   Serializer->WriteString("5");
   Serializer->EndElement();
   Serializer->StartElement("NumberTwo","","","");
   Serializer->WriteString("10");
   Serializer->EndElement();
   Serializer->EndElement();
   Serializer->EndBody();
   Serializer->EndEnvelope();
  
   // Send the message to the XML Web service.
   Connector->EndMessage();     

   // Read the response.
   Reader.CreateInstance(__uuidof(SoapReader30));

   // Connect the reader to the output stream of the connector object.
   Reader->Load(_variant_t((IUnknown*)Connector->OutputStream), "");

   // Display the result.
   printf("Answer: %s\n", (const char*)Reader->RpcResult->text);
   
}

int main()
{
   CoInitialize(NULL);
   Add();
   CoUninitialize();
   return 0;
}

更改 EndPointURL 属性的值. 在URL里指定你的服务器名.

OK

总结一下必要的关键步骤
1.导入类型库

2.需要创建一个SoapConnector

3.下一步创建SoapSerializer

4.下一步把消息附加到SoapConnector的输入流

5.下一步读取结果.要读取服务器的回复,客户端应用需要使用SoapReader,

6.SoapReader被连接到SoapConnector输出流

7.用IXMLDOMElement对象可以从SoapReader里读到服务器的回复

posted on 2004-04-02 17:44 陈震军 阅读(6330) 评论(22)  编辑 收藏 网摘

Feedback

http://MyServer/Soap3DocSamples/DocSample1/Server/DocSample1.wsdl",能看看吗??
  回复  引用    

Connector->Property["EndPointURL"] = "http://localhost/THDP/WebService/WS_ReturnResult.wsdl";
这一句报错,我的webservice 是.net编写的。在IE里面可以打开。
不知道能不能 交流一下呵呵呵 说白了就是教教我 怎么用啦
  回复  引用    

忘说了我的联系方式 msn:rsrmjin@msn.com Email:rsrm@ma65.com
  回复  引用    

#4楼 [楼主] 2005-05-18 12:58 陈震军      
用VC++.2003里右键点项目名,“添加Web引用”,自动生成调用Web Services的 .H 的头文件,其实就是调用的类,完全不用自己写Connector->Property["EndPointURL"] , VC6.0里也可试试把这个文件拷贝过去用(我现在没有VC6了,没法试), 另外,如果你调用的是C#写的Web Services,把WS_ReturnResult.wsdl改成WS_ReturnResult.asmx试试

  回复  引用  查看    

谢谢
  回复  引用    

我现在是调用能通过,但是printf("Answer: %s\n", (const char *)Reader->RpcResult->firstChild->text);打印出来的是Answer: soap:Client 不知道事什么原因,在.net中启动调试,也没有反映是不是 我的写错了,你有什么聊天工具,我把我的代码 给你 你能帮我看看吗?谢谢
  回复  引用    

下面是我的代码
ISoapSerializerPtr Serializer;
ISoapReaderPtr Reader;
ISoapConnectorPtr Connector;

// Connect to the service

Connector.CreateInstance(__uuidof(HttpConnector30));
Connector->Property["EndPointURL"] = "http://localhost/THDP/WebService/WS_ReturnResult.asmx";
Connector->Connect();//和服务器连接

// Begin message
//Connector->Property["SoapAction"] = "http://tempuri.org/Add";//不知道是否必要
Connector->BeginMessage();

Serializer.CreateInstance(__uuidof(SoapSerializer30));

// 将serializer连接到connector的输入字符串
Serializer->Init(_variant_t((IUnknown*)Connector->InputStream));

// 创建SOAP消息
Serializer->StartEnvelope("SOAP","","");
Serializer->StartBody("");
Serializer->StartElement("Fun_Add","","","THDP.WebService");
//Serializer->StartElement("username","","","");
//Serializer->WriteString("2005-5-18 12:20:31");
//Serializer->WriteString("30");
//Serializer->EndElement();
Serializer->EndElement();
Serializer->EndBody();
Serializer->EndEnvelope();

// Send the message to the web service
Connector->EndMessage();

// 读取响应
Reader.CreateInstance(__uuidof(SoapReader30));

// 将reader联接到connector的输出字符串
Reader->Load(_variant_t((IUnknown*)Connector->OutputStream), "");

//cout<<(const char *)Reader->RpcResult->text);
//const char * str = (const char *)Reader->RpcResult->firstChild->text;
printf("Answer: %s\n", (const char *)Reader->RpcResult->firstChild->text);
  回复  引用    

#8楼 [楼主] 2005-05-25 09:34 陈震军      
//Connector->Property["SoapAction"] = "http://tempuri.org/Add";//不知道是否必要
Connector->BeginMessage();

这个当然必要啦!!连请求都没有怎么会有响应?在VC7里是这样写的:SendRequest(_T("SOAPAction: \"http://www.czj.com.in/OneShopData\"\r\n"));

你的Web Services的NameSpace没写,在XP/2003的机器上也许可以发送成功,但好像在2000下会失败的 ("http://tempuri.org..." 是本地用的, 不知道为何这样的NameSpace C#写的客户端却能成功调用,但VC6就会失败)

你的问题应该是发送的消息不正确,所以响应当然是空的(因为是空的,所以输出时会造成报格式错误或数据类型错误)
  回复  引用  查看    

#9楼  2005-06-04 23:37 afontane [未注册用户]
问题是客户端如果是用VB6或其他非DOTNET写的客户端要调用你的webservice怎么办呢?应该可以写成一个dll的,而且不需要引用MSSOAPLib30的,如有兴趣,可以交流一下。
msn:wangyong(AT)szonline.net
  回复  引用    

#10楼 [楼主] 2005-06-06 08:45 陈震军      
VC6和VC7.net非托管意思一样. 不用Toolkit,但还是要SOAP的API.
C#写的Web Services与这些非.Net的交互只要使用基本数据类型交互就可,避免使用数据集DataSet就可以了。其实DataSet也可和Recoreset交互的,详见微软的文档。不过格式有所不同,我使用的时候还要用XSL进行转换.
  回复  引用  查看    

#11楼  2005-06-10 16:29 jk [未注册用户]
Build the SOAP Message.这一段如何理解?
  回复  引用    

#12楼  2005-06-10 16:39 jk [未注册用户]
Serializer->StartEnvelope("","","");
Serializer->StartBody("");
Serializer->StartElement("AddNumbers","http://tempuri.org/DocSample1/message/","",""); //这是本地的Web Services,实际中要指定命名空间
Serializer->StartElement("NumberOne","","","");
Serializer->WriteString("5");
Serializer->EndElement();
Serializer->StartElement("NumberTwo","","","");
Serializer->WriteString("10");
Serializer->EndElement();
Serializer->EndElement();
Serializer->EndBody();
Serializer->EndEnvelope();
这一段中,Start与End必须配对吗?而且必须针对web服务中的每个方法去编写SOAP消息吗?有没有通用的方法来调用web 服务?
麻烦高手指教,谢谢!
  回复  引用    

#13楼  2005-09-29 14:33 11 [未注册用户]
webservice中 方法AddNumbers得到的 参数 NumberOne 和 NumberTwo 的值怎么都是0呢? 无论客户端传什么上去都一样。

我的webservice在XP(sp2)的IIS上的,是否跟 什么设置有关?
  回复  引用    

#14楼  2005-10-14 12:37 gch [未注册用户]
我的一运行就出现:
Runtime Error!
Program:D:\develop\testws\debug\testws.exe
abnomal program termination

这样的提示错误!不知道是怎么回事?高人可以指教一下么?
  回复  引用    

#15楼  2005-10-14 12:40 gch [未注册用户]
跟踪时,应该是到了这一句出错
Connector->EndMessage();

  回复  引用    

Connector->EndMessage();
出现错误,原因在哪,我也很想知道,谢谢了
  回复  引用    

#17楼  2005-11-18 12:32 罗明貌 [未注册用户]
我也是出现上楼上相同的错误,麻烦哪位高手解答一下啊。
  回复  引用    

我用了
Connector.CreateInstance(__uuidof(HttpConnector30));
进行连接,为什么提示连接失败?
  回复  引用    

另外,我的服务器端是用Java开发的,采用Axis即时部署,请问在
Serializer->StartElement
中我的命名空间是什么?
  回复  引用    

您好,我是用VC6.0调用WebService,调用能够成功,可是却返回soap:Server,您能帮我看看吗?代码如下:
// WebServiceTest.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <stdio.h>

void query(char *EndPointURL,char *NameSpace,char *method,int argv_len,char* args[],char* argv[])
{

ISoapSerializerPtr Serializer;
ISoapReaderPtr Reader;
ISoapConnectorPtr Connector;

// Connect to the service

Connector.CreateInstance(__uuidof(HttpConnector30));
Connector->Property["EndPointURL"] = EndPointURL;//"http://localhost/WebService1/Service1.asmx";
Connector->Connect();//和服务器连接

// Begin message
Connector->Property["SoapAction"] = _bstr_t(NameSpace)+"/"+_bstr_t(method);//"http://xin.org/getIP";
Connector->BeginMessage();

Serializer.CreateInstance(__uuidof(SoapSerializer30));

// 将serializer连接到connector的输入字符串
Serializer->Init(_variant_t((IUnknown*)Connector->InputStream));

// 创建SOAP消息
Serializer->StartEnvelope("soap","","");
Serializer->StartBody("body");
Serializer->StartElement(method,NameSpace,"","");//命名空间必须有

for(int i=0;i<argv_len;i++)
{
Serializer->StartElement(args[i],NameSpace,"","");
Serializer->WriteString(argv[i]);
Serializer->EndElement();
}
Serializer->EndElement();

Serializer->EndBody();
Serializer->EndEnvelope();

// Send the message to the web service
Connector->EndMessage();

// 读取响应
Reader.CreateInstance(__uuidof(SoapReader30));

// 将reader联接到connector的输出字符串
Reader->Load(_variant_t((IUnknown*)Connector->OutputStream), "");

//cout<<(const char *)Reader->RpcResult->text);
//const char * str = (const char *)Reader->RpcResult->firstChild->text;

printf("Answer: %s\n",(const char*)Reader->RpcResult->xml);

}


int main(int argc, char* argv[])
{
CoInitialize(NULL);

char *EndPointURL="http://www.wopos.com/webservice/weather.asmx";
char *NameSpace="http://tempuri.org";
char *method="getWeather";
int argv_len=1;
char* args[1]={"mCity"};
char* argu[1]={"济南"};
query(EndPointURL,NameSpace,method,argv_len,args,argu);

CoUninitialize();
getchar();
return 0;

}



  回复  引用    

#21楼  2007-09-26 15:29 HAI [未注册用户]
@恐怖的webservice
想知道Answer:soap:Client的问题怎么解决的?
msn luceker@hotmail.com 谢谢
  回复  引用    

#22楼  2007-10-19 10:34 HAI [未注册用户]
我用vc MFC编程,中间实现soap通讯的客户端开发,考虑到可配置性,将服务端的url设置成可变的,但是问题出现了,如果把服务器地址改成一个不正确的,在开始Connector->Connect()时 能够成功,到了中间写信息,然后进行Connector->EndMessage()时程序弹出提示框 然后程序关闭,我怎么来截取错误,而且不让程序关闭呢?
用if(Connector->EndMessage() !=0)来判断? 我试着怎么不行啊?

望各位指点
msn luceker@hotmail.com
luceker@yahoo.com.cn
  回复  引用    





标题  
姓名  
主页
Email (博主才能看到) 
验证码 *  看不清,换一张 [登录][注册]
内容(请不要发表任何与政治相关的内容)  
  登录  使用高级评论  新用户注册  返回页首  恢复上次提交      
该文被作者在 2004-04-02 22:40 编辑过
Google站内搜索


China-pub 计算机图书网上专卖店!6.5万品种 2-8折!
近千种 9-95 新二手计算图书火热销售中!

相关文章:

相关链接: