首页 > 开发 > 综合 > 正文

利用PocketSOAP链接webservice(2)---VC++

2024-07-21 02:21:44
字体:
来源:转载
供稿:网友
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_)
  • 本文来源于网页设计爱好者web开发社区http://www.html.org.cn收集整理,欢迎访问。
  • 发表评论 共有条评论
    用户名: 密码:
    验证码: 匿名发表