console app HttpClient post to mvc web api

后端 未结 1 335
既然无缘
既然无缘 2021-01-12 17:59

I have a default mvc web api instance built from the Visual Studio 2012 template. It has the following routes and post method in the default ValuesController - the MVC site

相关标签:
1条回答
  • 2021-01-12 18:45

    Try the following code:

    class Program {
    
        static void Main(string[] args) {
    
            HttpClient client = new HttpClient();
            var content = new StringContent("=Here is my input string");
            content.Headers.ContentType = new MediaTypeHeaderValue("application/x-www-form-urlencoded");
            client.PostAsync("http://localhost:2451/api/values", content)
                .ContinueWith(task => {
    
                    var response = task.Result;
                    Console.WriteLine(response.Content.ReadAsStringAsync().Result);
                });
    
            Console.ReadLine();
        }
    }
    

    Have a look at the "Sending Simple Types" section of this blog post: http://www.asp.net/web-api/overview/working-with-http/sending-html-form-data,-part-1

    0 讨论(0)
提交回复
热议问题