利用PocketSOAP链接webservice(2)---VC++
发表于:2007-06-30来源:作者:点击数:
标签:
psTemp.cpp _______________________ // psTemp.cpp : Defines the entry point for the console application. // #include s td afx.h #include resource.h int _tmain(int argc, LPCTSTR argv[]) { USES_CONVERSION ; std::cout pocket SOA P Temperature d
psTemp.cpp
_______________________
// psTemp.cpp : Defines the entry point for the console application.
//
#include "s
tdafx.h"
#include "resource.h"
int _tmain(int argc, LPCTSTR argv[])
{
USES_CONVERSION ;
std::cout << "pocket
SOAP 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"), CComVari
ant(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_S
TDAFX_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_)
原文转自:http://www.ltesting.net