VC6.0使用SOAP調用公共服務

1.首先安裝Microsoft SOAP toolkit3

2.比如我們要調用的這個

http://fy.webxml.com.cn/webservices/EnglishChinese.asmx?op=TranslatorString

他的

SOAP 1.2

以下是 SOAP 1.2 請求和響應示例。所顯示的佔位符需替換爲實際值。


POST /webservices/EnglishChinese.asmx HTTP/1.1
Host: fy.webxml.com.cn
Content-Type: application/soap+xml; charset=utf-8
Content-Length: length


<?xml version="1.0" encoding="utf-8"?>
<soap12:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap12="http://www.w3.org/2003/05/soap-envelope">
  <soap12:Body>
    <TranslatorString xmlns="http://WebXml.com.cn/">
      <wordKey>string</wordKey>
    </TranslatorString>
  </soap12:Body>
</soap12:Envelope>

// 對應的 build SOAP msg 要對應起來
#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;
void Run()
{
   ISoapSerializerPtr Serializer;
   ISoapReaderPtr Reader;
   ISoapConnectorPtr Connector;
   // Connect to the service.
   Connector.CreateInstance(__uuidof(HttpConnector30));
   Connector->Property["EndPointURL"] = "http://fy.webxml.com.cn/webservices/EnglishChinese.asmx";
   Connector->Connect();


   // Begin the message.
   //Connector->Property["SoapAction"] = "uri:AddNumbers";
   Connector->Property["SoapAction"] = "http://WebXml.com.cn/TranslatorString";
   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("SOAP","","");
   Serializer->StartBody("");
   Serializer->StartElement("TranslatorString","http://WebXml.com.cn/","","");
   Serializer->StartElement("wordKey","http://WebXml.com.cn/","","");
   Serializer->WriteString("dad");
   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);
   Run();
   CoUninitialize();
   return 0;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章