如何调用Web API Post方法?

时间:2013-12-17 23:21:35

标签: c# post asp.net-web-api http-post windows-ce

我需要添加一个Post方法,并且已经开始了:

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;
    }
}

发送此uri需要做什么才能处理?

注意:如果它有任何区别,则客户端是Windows CE / .NET 3.5项目。我使用JSON.NET

1 个答案:

答案 0 :(得分:3)

您需要在请求上调用GetResponse()方法来实际进行调用,并收到响应:

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