日期:2012-09-14  浏览次数:20586 次

psTemp.cpp
_______________________
// psTemp.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include "resource.h"


int _tmain(int argc, LPCTSTR argv[])
{
    USES_CONVERSION ;

    std::cout << "pocketSOAP Temperature demo" << std::endl ;

    // test useage from the MTA, swap these over to test usage from a STA
    // CoInitializeEx(0,  COINIT_APARTMENTTHREADED ) ;
    CoInitializeEx(0,  COINIT_MULTITHREADED ) ;
    {
        // the zip code to find the temperature for
        CComBSTR zip (L"94107") ;
        if ( argc > 1 )
            zip = argv[1] ;

        // create the Envelope object
        CComPtr<ISOAPEnvelope> e ;
        HRESULT hr = e.CoCreateInstance(__uuidof(CoEnvelope)) ;
        if ( FAILED(hr))
        {
            std::cout << "Failed to create Envelope object, hr=" << std::hex << hr << std::endl ;
            return -1;
        }

        // set the methodName and namespace URI
        e->SetMethod ( CComBSTR("getTemp"), CComBSTR("urn:xmethods-Temperature") ) ;

        // get the parameters collection
        CComPtr<ISOAPNodes> p ;
        e->get_Parameters(&p) ;

        // create a new parameter
        p->Create ( CComBSTR(L"zipcode"), CComVariant(zip), NULL, NULL, NULL, NULL ) ;
        p.Release() ;

        // create the transport object
        CComPtr<IHTTPTransport> t ;
        hr = t.CoCreateInstance(__uuidof(HTTPTransport)) ;
        if (FAILED(hr))
        {
            std::cout << "Failed to create HTTP object, hr=" << std::hex << hr << std::endl ;
            return -1 ;
        }
        // set the SOAPAction header
        t->put_SOAPAction(CComBSTR(L"")) ;

        // serialize the envelope
        CComBSTR env ;
        hr = e->Serialize(&env) ;
        if (FAILED(hr))
        {
            std::cout << "Failed to serialize en