请大家帮帮忙(如何在VC6.0中调用Web Service):
请大家帮帮忙(如何在VC6.0中调用Web Service):
以下是我在.NET 2005中用C#写的Web Service代码:
using System;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
[WebService(Namespace = "http://tempuri.org/ ")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
public class Service : System.Web.Services.WebService
{
public Service () {
//如果使用设计的组件,请取消注释以下行
//InitializeComponent();
}
[WebMethod]
public string HelloWorld() {
return "Hello World ";
}
[WebMethod]
public int Add(int a, int b)
{
int c = a + b;
return c;
}
}
而在VC 6.0中的代码如下:
#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));
Connector-> Property[ "EndPointURL "] = "http://localhost/123/Service.asmx ";
Connector-> Connect();
// Begin the message.
//Connector-> Property[ "SoapAction "] = "uri:AddNumbers ";
Connector-> Property[ "SoapAction "] = "http://tempuri.org/Service.HelloWorld ";
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( "Add ", "http://tempuri.org/Service.asmx ", " ", " ");
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;
}
为什么打印出来的结果是: Answer: Soap: Client, 而不是我预期的: Answer: Hello World呢?
如果要调用我在Web Service 里写的Add 方法, 如何调用呢(参数及返回结果)?
[解决办法]
http://www.chengxu123.cn/detailt_53794.html
[解决办法]
http://www.cs.fsu.edu/~engelen/soap.html
[解决办法]
http://www.codeproject.com/soap/VSOAPClient.asp