Search code examples
.netwpfashx

How to call .ashx handler from WPF Application?


I have .ashx generic handler on server and I want to call it from WPF application to retrieve some information, is it possible?


Solution

  • You can use the System.Net.WebClient class to access a 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);
    
    }
    

    Here you can find the MSDN documentation.