c++ 利用msxml2.dll请求WebService

#pragma once
#include 
"stdafx.h"
#include 
"Atlbase.h"
//#import "msxml.dll"
#import "msxml2.dll"

using namespace MSXML2;
int main(int argc, char* argv[])
{
    printf(
"Test of XMLHTTP by masterz!\n");
    CoInitialize(NULL);
    
try
    
{
        IXMLHTTPRequestPtr xmlrequest;
//定义http请求对象
        
//xmlrequest.CreateInstance("Msxml2.XMLHTTP");
        xmlrequest.CreateInstance(__uuidof(XMLHTTP));//创建实列
        CComVariant vFalse(FALSE);
        CComVariant vNull(NULL);
        xmlrequest
->open("POST",bstr_t("http://localhost:1135/Service1.asmx/GetName"),vFalse,vNull,vNull);//打开WEBServeice方法GetName
        xmlrequest->setRequestHeader("Content-Type:","application/x-www-form-urlencoded");//设置Http头
        _bstr_t bsdata("name=xiaowy&password=kkkk");//use your login name 传入请求参数
        xmlrequest->send(_variant_t(bsdata));//发道数据
        BSTR bstrbody;
        xmlrequest
->get_responseText(&bstrbody);//得到返回数据
        _bstr_t bstrtbody(bstrbody);
        printf(
"%s\n",(LPCTSTR)bstrtbody);
        
//MessageBox(0,(LPCTSTR)bstrtbody,"",MB_OK);
        MSXML2::IXMLDOMDocument2Ptr m_xmldoc;
        m_xmldoc.CreateInstance(__uuidof(MSXML2::DOMDocument));
        m_xmldoc
->loadXML(bstrbody);
        
/*MSXML2::IXMLDOMNodeListPtr nodesPtr;
        nodesPtr = m_xmldoc->selectNodes(_bstr_t("string"));
        LONG * length;
        HRESULT r =  nodesPtr->get_length(length);
*/

        
//MSXML2::IXMLDOMNodePtr nodePtr =
        
//BSTR * str;
        
//HRESULT result = nodePtr->get_text(str);
        MSXML2::IXMLDOMNodePtr node = m_xmldoc->documentElement->firstChild;
        LPCTSTR str 
= (LPCTSTR)node->nodeName;
        LPCTSTR str2
=(LPCTSTR)m_xmldoc->documentElement->text;
        printf(
"%s",str2);
        getchar();
    }

    
catch (_com_error &e)
    
{
        printf(
"Description = '%s'\n", (char*) e.Description());
    }

    CoUninitialize();
    printf(
"program end\n");
    
return 0;
}
posted @ 2008-07-22 01:39  吴碧宇  阅读(2170)  评论(0编辑  收藏  举报