如何从WPF应用程序调用.ashx处理程序?

Lev*_*Lev 2 .net wpf ashx

我在服务器上有.ashx通用处理程序,我想从WPF应用程序调用它来检索一些信息,是否可能?

Wou*_*ort 5

您可以使用System.Net.WebClient类来访问URL.

using (WebClient client = new WebClient())
{
    client.Headers["User-Agent"] = "Mozilla/4.0 (Compatible; Windows NT 5.1; MSIE 6.0)" +
    " (compatible; MSIE 6.0; Windows NT 5.1; .NET CLR 1.1.4322; .NET CLR 2.0.50727)";

    // Download data.
    byte[] arr = client.DownloadData("http://www.yourserver.com/"); // url for .ashx file

    // Write values.
    Console.WriteLine("--- WebClient result ---");
    Console.WriteLine(arr.Length);

}
Run Code Online (Sandbox Code Playgroud)

在这里,您可以找到MSDN文档.