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

IXMLHTTPRequest problem

P: 8
Hi all,

I have developed an application and in them i want save some files in a remote
server. 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 application(Ethereal) und 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.
Thanks in advance

Alireza
Sep 14 '08 #1
Share this Question
Share on Google+
9 Replies


acoder
Expert Mod 15k+
P: 16,027
Can you post the code you're using. It may help debug the problem.
Sep 14 '08 #2

P: 8
Can you post the code you're using. It may help debug the problem.
Hi acoder, and thank you for your reply.

Here is the code of two functions that I'm using. I've omitted some codes those are not important such as reporting errors to make shorter message :


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.6.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.6.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.  
thanks a lot.
Alireza
Sep 15 '08 #3

P: 8
Hi acoder,

I must be tell that the previous code was my first try in synchronous form, but I've tried to send the request in asynchronous mode.
Here is the code:

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

acoder
Expert Mod 15k+
P: 16,027
OK, this is not JavaScript. It seems like you're using Web DAV. I have no experience of it, so can't be much help, but if you decide to use JavaScript, I can help. Maybe someone else who has used WebDAV will be able to assist you here.
Sep 15 '08 #5

P: 8
OK, this is not JavaScript. It seems like you're using Web DAV. I have no experience of it, so can't be much help, but if you decide to use JavaScript, I can help. Maybe someone else who has used WebDAV will be able to assist you here.
Thanks for your reply. Really I'm new in "bytes". Can you recommend me, in which
forum may i become any tips to resolve my problem.

thank a lot.
Alireza
Sep 15 '08 #6

acoder
Expert Mod 15k+
P: 16,027
Correct me if I'm wrong, but this seems like C++ code. If so, you can post in the C/C++ forum.
Sep 15 '08 #7

P: 8
Correct me if I'm wrong, but this seems like C++ code. If so, you can post in the C/C++ forum.
Yes, it is and thanks for your effort. I do so.

Alieza
Sep 16 '08 #8

acoder
Expert Mod 15k+
P: 16,027
OK, good luck in solving your problem.
Sep 16 '08 #9

rnd me
Expert 100+
P: 427
msxml4 is kinda crappy if i remember correctly.

ok, checked and i was right.

try using 6 or 3 instead.

check out the rundown on this page
Sep 16 '08 #10

Post your reply

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