2012-05-23 111 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。它基本上以相同的方式工作。

相關問題