2011-05-19 25 views
2

我現在有一個項目轉換ASP經典代碼,以C#,然後我進了此部分代碼...Microsoft.XMLHTTP在C#.NET

Function sendRequest(sRequest) //sRequest is XML data 
    Dim sResponse 

    Set oHTTP = Server.CreateObject("Microsoft.XMLHTTP") 

    oHTTP.open     HTTP_POST, WDL_URL_PREFIX, false 
    oHTTP.setRequestHeader  "Content-Type", "application/x-www-form-urlencoded" 
    oHTTP.send     "IN=" & Server.UrlEncode(sRequest) 

    sResponse = oHTTP.responseText 
    sendRequest = sResponse 
    End Function 

功能基本上通過HTTP發送XML數據和它使用Microsoft.XMLHTTP對象。現在,什麼是.NET中這個對象(Microsoft.XMLHTTP)相當於,因爲我不想用這個傳統的經典DLL ...

謝謝...

回答

4

在.NET中,最簡單的實現還有很簡單:

string url = ..., request = ...; 
using (var client = new WebClient()) 
{ 
    var response = client.UploadValues(url, new NameValueCollection { 
     {"IN",request} 
    }); 
    var text = client.Encoding.GetString(response); 
} 

我使用C#在這裏,但它適用於VB了。

+0

謝謝,但我在新NameValueCollection {{「IN」,請求/ /錯誤超載 – dotnetlinc 2011-05-19 09:44:17

+0

+ 1錯誤,你說得對WebClient比這個用例更適合WebRequest。 (刪除了我的答案。) – Heinzi 2011-05-19 10:03:46