Web API and WPF Client

I completed the following article to set up a simple Web API solution: http://www.codeproject.com/Articles/350488/A-simple-POC-using-ASP-NET-Web-API-Entity-Framewor

I skipped the Common, Log4Net, and Castle Windsor project to make the project as simple as possible.

Then I created a WPF project. However, now, which project should I reference for access to WebAPI and basic models?

+4
source share
1 answer

Use the HttpWebRequest class to request a web API. Below is a quick example for something that I used to query any other holiday service (this service only allowed POST / GET, not DELETE / PUT).

        HttpWebRequest request = WebRequest.Create(actionUrl) as HttpWebRequest; 
        request.ContentType = "application/json";

        if (postData.Length > 0)
        {
            request.Method = "POST"; // we have some post data, act as post request.

            // write post data to request stream, and dispose streamwriter afterwards.
            using (StreamWriter writer = new StreamWriter(request.GetRequestStream()))
            {
                writer.Write(postData);
                writer.Close();
            }
        }
        else
        {
            request.Method = "GET"; // no post data, act as get request.
            request.ContentLength = 0;
        }

        string responseData = string.Empty;

        using (HttpWebResponse response = request.GetResponse() as HttpWebResponse)
        {
            using (StreamReader reader = new StreamReader(response.GetResponseStream()))
            {
                responseData = reader.ReadToEnd();
                reader.Close();
            }

            response.Close();
        }

        return responseData;

nuget, " Microsoft ASP.NET Web API", WebAPI. (http://www.asp.net/web-api/overview/web-api-clients/calling-a-web-api-from-a-net-client)

+5

All Articles