Asynchronous MSXML2 XMLHTTP Request in code behind

3.3k views Asked by At

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#.

Any help would be greatly appreciated!

2

There are 2 answers

0
Laird Streak On BEST ANSWER
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
        }
    }
0
Ben On

In a WinForms application use a WebRequest instead. It basically works the same way.