Failed to CreateInstance while using IXMLHttpRequestPtr
-
Hello, I am using MSXML to get HTTP response from a webserver. The problem is when I use pIXMLHTTPRequest.CreateInstance("Microsoft.XMLHTTP"); it always goes to error and can't continue to open the connection. Can someone tell me where possibly the problem is, and why should I create the instance? Many thanks in advance. ***************************************** BOOL CAPIDlg::MakeRequest() { BOOL bRetVal=FALSE; BSTR Result=NULL; try{ MSXML::IXMLHttpRequestPtr pIXMLHTTPRequest; HRESULT hResult; hResult = pIXMLHTTPRequest.CreateInstance("Microsoft.XMLHTTP"); //Test the connection if ( FAILED(hResult) ) { MessageBox("Fail to create the connection"); return bRetVal; } MessageBox("Continue anyway"); //Open the connection pIXMLHTTPRequest->open("POST",(_bstr_t)"www.yahoo.com"); //Send the request pIXMLHTTPRequest->send(); MessageBox("Success to open"); //Get the result Result=pIXMLHTTPRequest->responseText; if(Result) //Display the result {m_Result.SetWindowText((_bstr_t)Result); } else //Report error MessageBox("Can't make the connection"); } catch(...) { //Report error MessageBox("Can't initialize the HTTP request"); } return true; }
-
Hello, I am using MSXML to get HTTP response from a webserver. The problem is when I use pIXMLHTTPRequest.CreateInstance("Microsoft.XMLHTTP"); it always goes to error and can't continue to open the connection. Can someone tell me where possibly the problem is, and why should I create the instance? Many thanks in advance. ***************************************** BOOL CAPIDlg::MakeRequest() { BOOL bRetVal=FALSE; BSTR Result=NULL; try{ MSXML::IXMLHttpRequestPtr pIXMLHTTPRequest; HRESULT hResult; hResult = pIXMLHTTPRequest.CreateInstance("Microsoft.XMLHTTP"); //Test the connection if ( FAILED(hResult) ) { MessageBox("Fail to create the connection"); return bRetVal; } MessageBox("Continue anyway"); //Open the connection pIXMLHTTPRequest->open("POST",(_bstr_t)"www.yahoo.com"); //Send the request pIXMLHTTPRequest->send(); MessageBox("Success to open"); //Get the result Result=pIXMLHTTPRequest->responseText; if(Result) //Display the result {m_Result.SetWindowText((_bstr_t)Result); } else //Report error MessageBox("Can't make the connection"); } catch(...) { //Report error MessageBox("Can't initialize the HTTP request"); } return true; }
Are you calling CoInitialize? Ref: Applications must initialize the COM library before they can call COM library functions. From the MSDN library. HRESULT hr = CoInitialize(NULL); bool bRetVal; bRetVal=false; IXMLHttpRequestPtr pIXMLHTTPRequest; HRESULT hResult; hResult = pIXMLHTTPRequest.CreateInstance("Microsoft.XMLHTTP"); //Test the connection if ( FAILED(hResult) ) { MessageBox("Fail to create the connection"); return bRetVal; } Workes fine for in a simple dialog initialization test. "I will find a new sig someday."
-
Are you calling CoInitialize? Ref: Applications must initialize the COM library before they can call COM library functions. From the MSDN library. HRESULT hr = CoInitialize(NULL); bool bRetVal; bRetVal=false; IXMLHttpRequestPtr pIXMLHTTPRequest; HRESULT hResult; hResult = pIXMLHTTPRequest.CreateInstance("Microsoft.XMLHTTP"); //Test the connection if ( FAILED(hResult) ) { MessageBox("Fail to create the connection"); return bRetVal; } Workes fine for in a simple dialog initialization test. "I will find a new sig someday."
Thanks a lot, it helps!
-
Thanks a lot, it helps!
Hi! Will this code work with Managed C++/CLI also? What else need to be done to make this code work with Managed C++?
int _tmain(int argc, _TCHAR* argv[])
{
std::ostringstream XmlLogrequest;
XmlLogrequest << "<?xml version="
<< "\""
<< "1.0"
<< "\""
<< "?>"
<< "<request action = "
<< "\"registration"
<< "\""
<< ">"
<< "<element id="
<< "\"id001" << "\""
<< ">"//cout << XmlLogrequest.str();
IXMLHTTPRequestPtr pIXMLHTTPRequest = NULL;
BSTR bstrString = NULL;
HRESULT hr = CoInitialize(NULL);
string test = XmlLogrequest.str().substr(0,XmlLogrequest.str().size());
cout << test<<"\n";
try {
hr=pIXMLHTTPRequest.CreateInstance("Msxml2.XMLHTTP.3.0");
//SUCCEEDED(hr) ? 0 : throw hr;pIXMLHTTPRequest->open("POST", "https://live.jqk365.com/cgibin/EClientIntegration",false); // SUCCEEDED(hr) ? 0 : throw hr; hr=pIXMLHTTPRequest->send(test.c\_str()); 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("Error"), \_T("Error"), MB\_OK); if(bstrString) ::SysFreeString(bstrString); } return 0;
}