Asynchronous MSXML2 XMLHTTP Request in code behind

一曲冷凌霜 提交于 2019-12-13 07:17:11

问题


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!


回答1:


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
        }
    }



回答2:


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



来源:https://stackoverflow.com/questions/10715662/asynchronous-msxml2-xmlhttp-request-in-code-behind

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!