Search code examples
c#postasp.net-web-apihttp-postwindows-ce

How can I call a Web API Post method?


I need to add a Post method, and have gotten this start:

private void AddDepartment()
{
    // AccountId (int) and Name (string) are the two vals other than Id, which is auto-added on the server
    int onAccountOfWally = 42;
    string moniker = "Wild Billy Jack Black Stallion";
    Cursor.Current = Cursors.WaitCursor;
    try
    {
        string uri = String.Format("http://platypus:28642/api/Platypi/{0}/{1}", onAccountOfWally, moniker);
        var webRequest = (HttpWebRequest)WebRequest.Create(uri);
        webRequest.Method = "POST";
        //var webResponse = (HttpWebResponse)WebResponse. <-- there is no "Create" for this...
    }
    finally
    {
        Cursor.Current = Cursors.Default;
    }
}

What do I need to do to send this uri on up to be processed?

Note: If it makes any difference, the client is a Windows CE / .NET 3.5 project. I am using JSON.NET


Solution

  • You need to call GetResponse() method on the request to actually make the call, and receive the response:

    var webResponse = (HttpWebResponse)webRequest.GetResponse();