利用PocketSOAP連結webservice(2)---VC++

來源:互聯網
上載者:User
c++|web|連結 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 envelope, hr=" << std::hex << hr << std::endl;
            return -1 ;
        }

        // send to the server
        // add a call to t->SetProxy ( CComBSTR(L"MyProxy"), 8080 ) here if you
        // need to connect through a proxy server
        hr = t->Send ( CComBSTR(L"http://services.xmethods.net:80/soap/servlet/rpcrouter"), env ) ;
        if (FAILED(hr))
        {
            std::cout << "Failed to send request, hr=" << std::hex << hr << std::endl;
            return -1 ;
        }

        // parse the response
        hr = e->Parse ( CComVariant(t), CComBSTR(L"") ) ;
        if (FAILED(hr))
        {
            // extract the detailed error info if there is some
            CComPtr<IErrorInfo> e ;
            GetErrorInfo(0, &e) ;
            if ( e )
            {
                CComBSTR src, desc ;
                e->GetSource(&src) ;
                e->GetDescription(&desc) ;
                std::cout << "Error in response, got error " << OLE2A(desc) << " from " << OLE2A(src) << ", [hr=" << std::hex << hr << "]" << std::endl;
            }
            else
                std::cout << "Failed to parse response, [hr=" << std::hex << hr << "]" << std::endl;

            return -1 ;
        }

        // get the parameters collection again, to get the response parameters
        e->get_Parameters(&p) ;
        CComPtr<ISOAPNode> retNode ;

        // get the first node in the parameters collection
        p->get_Item(0, &retNode) ;

        // get the nodes value
        CComVariant v ;
        retNode->get_Value(&v) ;

        // change its type to a string
        v.ChangeType(VT_BSTR) ;

        // print the results !
        std::cout << "cuurent temperature for zipcode " << OLE2A(zip) << " is " << OLE2A(v.bstrVal) << std::endl ;
    }

    CoUninitialize();
    return 0;
}
StdAfx.cpp
______________________________________
#include "stdafx.h"
#include <atlimpl.cpp>
StdAfx.h
______________________________________
// stdafx.h : include file for standard system include files,
//  or project specific include files that are used frequently, but
//      are changed infrequently
//

#if !defined(AFX_STDAFX_H__5521FE78_653C_437D_B8E8_CB6F274F39A5__INCLUDED_)
#define AFX_STDAFX_H__5521FE78_653C_437D_B8E8_CB6F274F39A5__INCLUDED_

#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000

#define _WIN32_WINNT 0x403

#include <atlbase.h>
#include <iostream>

#import "c:\program files\simonfell\pocketsoap1.1\psoap32.dll" raw_interfaces_only named_guids no_namespace

// TODO: reference additional headers your program requires here

//{{AFX_INSERT_LOCATION}}
// Microsoft Visual C++ will insert additional declarations immediately before the previous line.

#endif // !defined(AFX_STDAFX_H__5521FE78_653C_437D_B8E8_CB6F274F39A5__INCLUDED_)

相關文章

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。