Tuesday, October 25, 2011

Reading XML from C++

Reading XML from a URL is easy with by MSXML::IXMLHttpRequest.

Sample code is available at:
http://msdn.microsoft.com/en-us/library/ms992615(v=exchg.65).aspx

However, there are some disadvantages to this interface, which does not give you access to the low level handles if you need to tweak anything. For example, some servers require you to authenticate with a certificate and IXMLHttpRequest provides no way to do this. This interface will also display errors to the user and prompt for required information, so it's not appropriate for an application that will run silently.

Therefore, Microsoft provides a second interface called IServerXMLHTTPRequesthttp://msdn.microsoft.com/en-us/library/windows/desktop/ms762278(v=VS.85).aspx. This interface inherits from IXMLHttpRequest, but it does not use WinInet and so is appropriate for use in servers. It also allows you to explicitly provide a certificate using SXH_OPTION_SELECT_CLIENT_SSL_CERT.

3 comments:

  1. Hello Sir. I am new to IServerXMLHTTPRequest. Can you provide me a sample code on how to call a secured http server? Thank you.

    ReplyDelete
  2. I have a current working code:

    CComPtr xmlHttp;

    xmlHttp.CoCreateInstance(__uuidof(ServerXMLHTTP));

    _bstr_t sMethod = _T("POST");
    _bstr_t sUrl = "http://example.com/something.cgi";

    xmlHttp->open(sMethod,sUrl,false);

    _bstr_t sPostData = ;

    xmlHttp->send((BSTR)sPostData);

    I can only use this on a non-https server. The server was changed to https. With this, I can no longer send file to the server. Can you guide me on how I can still send data to the server?

    ReplyDelete
  3. wipindipy08 - I'd start by checking the return values of your function calls. A secure connection sometimes has additional requirements for certificate management. IIRC, IServerXMLHTTPRequest also has some limitations on the certificate handling. There were certain kinds of connections it couldn't handle. Sorry, I wrote this article two years ago and my memory is pretty hazy.

    ReplyDelete