473,395 Members | 1,756 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

IXMLHTTPRequest problem

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
1 7297
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

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

Similar topics

4
by: Bura Tino | last post by:
Hi, I took a sample below almost verbatim from the MS MSXML 3.0 website and the line hr=pIXMLHTTPRequest.CreateInstance("Msxml2.XMLHTTP"); never succeeds. What am I doing wrong? It says...
1
by: JXP | last post by:
Hello, I am attempting to send url with '\n' in some place. the url is like this: "http://localhost/MyFile.xml?param1=hello\nbye\n" I put this url in CString class and I run this code:
8
by: Agam Mehta | last post by:
Hi, Everything works fine with ixmlhttprequest. It gives me "access violation" only when i am trying to release it from the memory (i.e pXMLHttpReq->Release()). Below is my code....
0
by: Joseph | last post by:
I have been using "XMLHTTPClass" in an asp.net module. It is properly referenced and was working up until recently. Now it is failing with the error message "QueryInterface for interface...
42
by: Greg | last post by:
Hi, I've designed a bookmark in Ajax / PHP that I will put soon on sourceforge.net. But I've got an very tricky bug. I try it on some computers with Internet Explorer/Windows, Firefox...
9
by: Eric Wallstedt | last post by:
I have a page that "logs" changes made to input fields using ajax to pass data to a cgi. I use POST and it works fine most of the time (all the time in IE). But it fails when I get the data from...
102
by: hug | last post by:
www.webmaster, was suggested that this ng could be a better place.] I've updated my test server to handle if-modified-since. I've noticed that the (old copies I run of) IE and Netscape seem...
2
by: bhushanbsc | last post by:
Hi, I am working on a project related to windows pocket pc 5.0 (win32). in that i am trying client-server communicationusing SOAP server. but facing problem on...
0
by: AlirezaShokoienia | last post by:
Hi all, I'm new in "bytes" and don't know that my question is related to this forum? Anyway... I wrote an application and have used form this component to "PUT" files in a remote server and in this...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.