How can I call a Web API Post method?

雨燕双飞 提交于 2019-12-13 05:31:03

问题


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


回答1:


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

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


来源:https://stackoverflow.com/questions/20646715/how-can-i-call-a-web-api-post-method

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!