By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
462,137 Members | 858 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 462,137 IT Pros & Developers. It's quick & easy.

IXMLHTTPRequest problem

P: 8
Hi all,

I have developed an C++ application with Visual Studio 2005 and in them i want save some files in a remote server using IXMLHTTPRequest component. usually before save the file, i read the directory tree in remote server and user can select the right destination directory and often everything is OK and the application works fine.Except unfortunately when the destination directory(or actually the full path) is too long, the "send" method does not return and readyState remain 2, but on other hand file is correctly saved in remote server.
In this way i have tried to use a network monitoring pplication(Ethereal) and i observed that a "PUT" request is sent to remote server and then "200" response is received form the server and according to this observation the file was saved correctly. I think that i have problem with my client(MSXML4.DLL) or any internet setting.

Everybody can give me any recommendations to resolve this problem.
Thanks in advance

Alireza
Sep 16 '08 #1
Share this Question
Share on Google+
1 Reply

P: 8
Hi,
After some days nobody has answered me. I write a piece of code that I'm using
it, that probably may be helpful. Or it's possible, that i have a misunderstanding to
selecting the C/C++ forum? Please hint me if it's true.

Here is the code:


Expand|Select|Wrap|Line Numbers
  1. void CWebDAVOperation::GetWebDAVDirTree()
  2. {
  3.     // Variables.
  4.     HRESULT hr; 
  5.     IXMLHTTPRequestPtr pRequest = NULL;
  6.  
  7.     long lStatus = 0;
  8.     BSTR bstrResp;
  9.     BSTR bstrResponseText;
  10.     CString strQuery;
  11.     CString strDir = skillInfo.m_strAgentDirURI + _T("/") + m_strCallLogDir;
  12.  
  13.     // Create an instance of the request object.
  14.     hr=pRequest.CreateInstance("Msxml2.XMLHTTP.4.0");
  15.     SUCCEEDED(hr) ? 0 : throw hr;
  16.  
  17.     try
  18.     {
  19.         // Open the XMLHTTPRequest object with the SEARCH method and
  20.         // specify that it will be sent asynchronously.
  21.         hr=pRequest->open("SEARCH", _bstr_t(strDir), false, _bstr_t(skillInfo.m_strAgentUserName) , _bstr_t(skillInfo.m_strAgentPassword));
  22.         SUCCEEDED(hr) ? 0 : throw hr;
  23.  
  24.         // Set the Content-Type header.
  25.         hr=pRequest->setRequestHeader((bstr_t)"Content-Type", (bstr_t)"text/xml");
  26.         SUCCEEDED(hr) ? 0 : throw hr;
  27.  
  28.         // Build the query for a hierarchical search.
  29.         strQuery = _T("<?xml version=\"1.0\"?><D:searchrequest xmlns:D = \"DAV:\" >");
  30.         strQuery += _T("<D:sql>SELECT \"DAV:href\" FROM scope('deep traversal of \"");
  31.         strQuery += strDir;
  32.         strQuery += _T("\"')");
  33.         strQuery += _T("WHERE \"DAV:ishidden\"=False AND \"DAV:isfolder\" = True");
  34.         strQuery += _T("</D:sql></D:searchrequest>");
  35.  
  36.         // Send the SEARCH method request.
  37.         hr=pRequest->send(_bstr_t(strQuery));
  38.         SUCCEEDED(hr) ? 0 : throw hr;
  39.  
  40.         // Get the response status.
  41.         pRequest->get_status(&lStatus);
  42.  
  43.         // An error occurred on the server.
  44.         if(lStatus >= 500)
  45.         {
  46.             // report the error...
  47.         }
  48.  
  49.         // The method request was successful.
  50.         else if (lStatus == 207)
  51.         {
  52.             // Variables.
  53.             MSXML2::IXMLDOMDocumentPtr pDOMDoc = NULL;
  54.             MSXML2::IXMLDOMNodeListPtr pDOMNodeList = NULL;
  55.             MSXML2::IXMLDOMNodePtr pDOMNode = NULL;
  56.             BSTR bstrRespText;
  57.  
  58.             // Create an instance of the DOM Document.
  59.             HRESULT hr = pDOMDoc.CreateInstance(__uuidof(MSXML2::DOMDocument));
  60.  
  61.             // Check the status of pointer creation.
  62.             if(FAILED(hr))
  63.             {
  64.                 // report the error...
  65.             }
  66.  
  67.             // Get the method response XML text.
  68.             pRequest->get_responseText(&bstrRespText);
  69.  
  70.             // Load the XML document with the response text.
  71.             pDOMDoc->loadXML(bstrRespText);
  72.  
  73.             // Build a list of the DAV:href XML nodes, corresponding to the folders
  74.             // returned in the search request. The DAV: namespace is typically
  75.             // assigned the a: prefix in the XML response body.
  76.             pDOMNodeList = pDOMDoc->getElementsByTagName((bstr_t)"a:prop");
  77.             //pDOMNodeList = pDOMDoc->getElementsByTagName((bstr_t)"a:href");
  78.  
  79.             // Display the number of folders found.
  80.             long lLen, lShowLen;
  81.             pDOMNodeList->get_length(&lLen);
  82.  
  83.             ::SysFreeString(bstrRespText);
  84.  
  85.             lShowLen = lLen;
  86.             // List the folders found.
  87.             for(int i=0; i<lLen;i++)
  88.             {
  89.                 pDOMNode = pDOMNodeList->nextNode();
  90.                 if(pDOMNode != NULL)
  91.                 {
  92.                     BSTR bstrText;
  93.                     pDOMNode->get_text(&bstrText);
  94.                     CString strText(bstrText);
  95.  
  96.                     // i save the folder name
  97.                     // ...
  98.  
  99.                     ::SysFreeString(bstrText);
  100.                 }
  101.             }
  102.         }
  103.         else
  104.         {
  105.             // report the lStatus
  106.         }
  107.  
  108.         pRequest = NULL;
  109.     }
  110.     catch(_com_error& e)
  111.     {
  112.  
  113.         // report the error message
  114.  
  115.         pRequest = NULL;
  116.  
  117.     }
  118. }
  119.  
  120. bool CWebDAVOperation::SaveStream(IStream* pIStream, CString strPath, CString strUserName, CString strPassword)
  121. {
  122.     HRESULT hr;
  123.     int WebDavStep = 0;
  124.     BSTR bstrString = NULL;
  125.     IXMLHTTPRequestPtr pIXMLHTTPRequest = NULL;
  126.     try
  127.     {
  128.         WebDavStep ++;
  129.         hr=pIXMLHTTPRequest.CreateInstance("Msxml2.XMLHTTP.4.0");
  130.         SUCCEEDED(hr) ? 0 : throw hr;
  131.  
  132.         bstrString =  _bstr_t(strPath);
  133.  
  134.         WebDavStep ++;
  135.         hr=pIXMLHTTPRequest->open("PUT", bstrString, false, _bstr_t(strUserName) , _bstr_t(strPassword));
  136.         SUCCEEDED(hr) ? 0 : throw hr;
  137.  
  138.         WebDavStep ++;
  139.         // Set the Content-Type header.
  140.         hr=pIXMLHTTPRequest ->setRequestHeader((bstr_t)"Content-Type", (bstr_t)"audio/mpeg3");
  141.         SUCCEEDED(hr) ? 0 : throw hr;
  142.  
  143.         WebDavStep ++;
  144.         // Set the Translate header to False.
  145.         hr=pIXMLHTTPRequest ->setRequestHeader((bstr_t)"Translate", (bstr_t)"f");
  146.         SUCCEEDED(hr) ? 0 : throw hr;
  147.  
  148.         WebDavStep ++;
  149.         hr=pIXMLHTTPRequest->send(pIStream);
  150.         SUCCEEDED(hr) ? 0 : throw hr;
  151.  
  152.         BSTR bstrValue = NULL;
  153.         WebDavStep ++;
  154.         hr=pIXMLHTTPRequest->get_responseText(&bstrValue);
  155.         CString Str1, Str2;
  156.         CString strResp(bstrValue);
  157.  
  158.         // report resonseText
  159.         //...
  160.  
  161.         ::SysFreeString(bstrValue);
  162.         bstrValue = NULL;
  163.  
  164.         pIXMLHTTPRequest = NULL;
  165.  
  166.         if(bstrString)
  167.         {
  168.              ::SysFreeString(bstrString);
  169.              bstrString = NULL;
  170.         }
  171.  
  172.         return true;
  173.     }
  174.     catch(_com_error& e)
  175.     {
  176.  
  177.         // report the error message
  178.  
  179.         pIXMLHTTPRequest = NULL;
  180.  
  181.     }
  182.  
  183.     return false;
  184. }
  185.  
and after some checks I've tried to use asynchronous mode with following code:


Expand|Select|Wrap|Line Numbers
  1. bool CWebDAVOperation::SaveStream(IStream* pIStream, CString strPath, CString strUserName, CString strPassword)
  2. {
  3.         HANDLE completedEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
  4.  
  5.         // ...
  6.  
  7.         WebDavStep ++;
  8.         hr=pIXMLHTTPRequest->open("PUT", bstrString, true, _bstr_t(strUserName) , _bstr_t(strPassword));
  9.         SUCCEEDED(hr) ? 0 : throw hr;
  10.  
  11.         // ...
  12.  
  13.         // Hook up the onreadystatechange event handler
  14.         IDispatch *sink = new XMLHttpEventSink(pIXMLHTTPRequest, completedEvent, &m_lstate);
  15.         hr=pIXMLHTTPRequest->put_onreadystatechange(sink);
  16.  
  17.         WebDavStep ++;
  18.         hr=pIXMLHTTPRequest->send(pIStream);
  19.         SUCCEEDED(hr) ? 0 : throw hr;
  20.  
  21.         // Since this is a console app the process would end if we just continued from here.
  22.         // So we wait until the async request is done (in real life, this means we didn't
  23.         // need an async request in the first place, but this is a sample, not real life).
  24.  
  25.         do
  26.         {
  27.             DWORD dwRetp = WaitForSingleObject(completedEvent, 1000);
  28.  
  29.             // report the m_lState (readyState)
  30.  
  31.             if(dwRetp == WAIT_OBJECT_0)
  32.                 break;
  33.         }while(true);
  34.  
  35.         sink->Release();
  36.  
  37.         CloseHandle(completedEvent);
  38.  
  39.         // ...
  40. }
  41.  
  42.  
  43. STDMETHODIMP XMLHttpEventSink::Invoke(DISPID dispIdMember, const IID &riid, LCID lcid, WORD wFlags, DISPPARAMS *pDispParams, VARIANT *pVarResult, EXCEPINFO *pExcepInfo, UINT *puArgErr)
  44. {
  45.     // Since this class isn't used for anything else, Invoke will get called only for onreadystatechange, and
  46.     // dispIdMember will always be 0.
  47.  
  48.     long state;
  49.     // Retrieve the state
  50.     _request->get_readyState(&state);
  51.     *m_plState = state;
  52.     //std::wcout << L"State: " << state << std::endl;
  53.  
  54.     if( state == 4 )
  55.     {
  56.         // The request has completed.
  57.         // Get the request status.
  58.  
  59.         //long status;
  60.         //_request->get_status(&status);
  61.  
  62.        // std::wcout << L"Status: " << status << std::endl;
  63.  
  64.         // Signal the main thread we're done.
  65.         SetEvent(_completedEvent);
  66.     }
  67.     return S_OK;
  68. }
I will be thankful if anyone can help me.
Thanks in advance.

Alireza
Sep 20 '08 #2

Post your reply

Sign in to post your reply or Sign up for a free account.