VC中使用IXMLHTTPRequest获取URL信息的问题,有代码*********************************************

ALong_Yue 2002-10-24 09:48:14
#import "msxml4.dll"
using namespace MSXML2;

void XMLHttpRequestSample()
{
IXMLHTTPRequestPtr pIXMLHTTPRequest = NULL;
BSTR bstrString = NULL;
HRESULT hr;

try {
hr=pIXMLHTTPRequest.CreateInstance("Msxml2.XMLHTTP.4.0");
SUCCEEDED(hr) ? 0 : throw hr;

hr=pIXMLHTTPRequest->open("GET", "http://XMLSampleServer/CatalogServer.asp", false);
SUCCEEDED(hr) ? 0 : throw hr;

hr=pIXMLHTTPRequest->send();
SUCCEEDED(hr) ? 0 : throw hr;

bstrString=pIXMLHTTPRequest->responseText;

MessageBox(NULL, _bstr_t(bstrString), _T("Results"), MB_OK);

if(bstrString)
{
::SysFreeString(bstrString);
bstrString = NULL;
}

} catch (...) {
MessageBox(NULL, _T("Exception occurred"), _T("Error"), MB_OK);
if(bstrString)
::SysFreeString(bstrString);
}

}
上面的代码,不能成功执行,在CreateInstance(...)退出,为什么?
...全文
172 点赞 收藏 4
写回复
4 条回复
切换为时间正序
当前发帖距今超过3年,不再开放新的回复
发表回复
2ndboy 2002-10-24
我也想知道
回复
masterz 2002-10-24
#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;
xmlrequest.CreateInstance("Msxml2.XMLHTTP");
_variant_t varp(false);
xmlrequest->open(_bstr_t("GET"),_bstr_t("http://www.csdn.net/expert/topic/855/855052.xml?temp=.176037"),varp);
xmlrequest->send();
BSTR bstrbody;
xmlrequest->get_responseText(&bstrbody);
_bstr_t bstrtbody(bstrbody);
printf("%s\n",(LPCTSTR)bstrtbody);

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


///////Post with XMLHTTP///////////////////////
#import "msxml.dll"
#import "msxml2.dll"
#include "Atlbase.h"
using namespace MSXML2;
int main(int argc, char* argv[])
{
printf("Test of XMLHTTP by masterz!\n");
CoInitialize(NULL);
try
{
IXMLHTTPRequestPtr xmlrequest;
//xmlrequest.CreateInstance("Msxml2.XMLHTTP");
xmlrequest.CreateInstance(__uuidof(XMLHTTP));
CComVariant vFalse(FALSE);
CComVariant vNull(NULL);
xmlrequest->open("POST",
_bstr_t("http://211.157.102.21/member/logon.asp"),vFalse,vNull,vNull);
xmlrequest->setRequestHeader("Content-Type:","application/x-www-form-urlencoded");
_bstr_t bsdata("name=xxx&pass=xxx&type=1");//use your login name and password
xmlrequest->send(_variant_t(bsdata));

BSTR bstrbody;
xmlrequest->get_responseText(&bstrbody);
_bstr_t bstrtbody(bstrbody);
printf("%s\n",(LPCTSTR)bstrtbody);
//MessageBox(0,bstrtbody,"",MB_OK);

}
catch (_com_error &e)
{
printf("Description = '%s'\n", (char*) e.Description());
}
CoUninitialize();
printf("program end\n");
return 0;
}
回复
kingzai 2002-10-24
//try these code
IXMLDOMDocumentPtr pXMLDOMDoc;
IXMLHttpRequestPtr pXMLHttpRequest;
bstr t bstrString;

try {
pXMLDOMDoc.CreateInstance( uuidof(DOMDocument));
pXMLDOMDoc->async=FALSE;

pXMLDOMDoc->loadXML("<root/>");
TESTHR(pXMLHttpRequest.CreateInstance("Microsoft.XMLHTTP"));

pXMLHttpRequest->open( bstr t(L"POST"), bstr t(L"http://localost/Teste/Sa
ve.asp"),false);
TESTHR(pXMLHttpRequest->send(pXMLDOMDoc->xml));

bstrString = pXMLHttpRequest->responseText;
} // try
catch(...)
//and I have found in MSDN Collection an example, but
it was with "GET' method
(http://msdn.microsoft.com/library/psdk/xmlsdk/xmld8bp0.htm).
回复
hjhhhl 2002-10-24
4848
回复
发帖
网络编程
创建于2007-09-28

1.8w+

社区成员

VC/MFC 网络编程
申请成为版主
帖子事件
创建了帖子
2002-10-24 09:48
社区公告
暂无公告