Search code examples
c#.netxmlhttprequestmsxml4

Asynchronous MSXML2 XMLHTTP Request in code behind


I want asynchronous HTTP call back to work in C# with MSXML2 APIs. I am calling this via a winform.

        x = new MSXML2.XMLHTTPClass();
        x.open("POST", "http://localhost/MyHandler.ashx", true, null, null);
        x.send("<test/>");
        x.onreadystatechange = ???? //// What to specify here in C#?
        var response = x.responseText; //// Works great synchronous!

I tried Action(), anonymous delegates, anonymous types but nothing works! Sadly on the internet this VB.NET Module driven solution exists but I am not sure how this can be done in C#.


Solution

  • try {
                System.Net.HttpWebRequest oHTTPRequest = System.Net.HttpWebRequest.Create("URL of Request") as System.Net.HttpWebRequest;
                System.Net.HttpWebResponse oHTTPResponse = oHTTPRequest.GetResponse as System.Net.HttpWebResponse;
                System.IO.StreamReader sr = new System.IO.StreamReader(oHTTPResponse.GetResponseStream);
                string respString = System.Web.HttpUtility.HtmlDecode(sr.ReadToEnd());
            } 
            catch (Exception oEX) 
            {
                //Log an Error
            }
        }