2012-05-23 144 views
0

我希望异步HTTP回调在C#中使用MSXML2 API工作。我通过一个winform来调用它。异步MSXML2 XMLHTTP Request in code behind

 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! 

我试过Action(),匿名代理,匿名类型但没有任何作用!可惜在互联网上这个VB.NET Module driven solution存在,但我不知道如何在C#中做到这一点。

任何帮助将不胜感激!

回答

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

WinForms应用程序中,改为使用WebRequest。它基本上以相同的方式工作。

相关问题