計(jì)算機(jī)二級(jí)C技巧:VC6調(diào)用WebService

字號(hào):

下面是個(gè)控制臺(tái)的樣例
    Toolkit3.0 終于給出VC6的樣例了,1.0只能看到VB和ASP的
    #include
    #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; //你機(jī)器得安裝SOAP Toolkit3.0 ,1.0時(shí),用using namespace時(shí)報(bào)錯(cuò)
    void Add()
    {
    ISoapSerializerPtr Serializer;
    ISoapReaderPtr Reader;
    ISoapConnectorPtr Connector;
    // Connect to the service.
    Connector.CreateInstance(__uuidof(HttpConnector30)); //HttpConnector30 失敗,無法這樣創(chuàng)建Connector,CXX0017 Error :Symbol “HttpConnector30“ not found(搖頭、嘆氣!)
    Connector->Property["EndPointURL"] = "http://MyServer/Soap3DocSamples/DocSample1/Server/DocSample1.wsdl"; //這個(gè)當(dāng)然得改成您自己的拉
    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,實(shí)際中要指定命名空間
    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里指定你的服務(wù)器名.
    OK
    總結(jié)一下必要的關(guān)鍵步驟
    1.導(dǎo)入類型庫
    2.需要?jiǎng)?chuàng)建一個(gè)SoapConnector
    3.下一步創(chuàng)建SoapSerializer
    4.下一步把消息附加到SoapConnector的輸入流
    5.下一步讀取結(jié)果.要讀取服務(wù)器的回復(fù),客戶端應(yīng)用需要使用SoapReader,
    6.SoapReader被連接到SoapConnector輸出流
    7.用IXMLDOMElement對(duì)象可以從SoapReader里讀到服務(wù)器的回復(fù)。