控制台应用程序的HttpClient发布到MVC的Web API

编程入门 行业动态 更新时间:2024-10-25 20:25:33
本文介绍了控制台应用程序的HttpClient发布到MVC的Web API的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我从Visual Studio 2012的模板建立了一个默认的MVC Web API实例。它在默认Values​​Controller以下途径和方法后 - MVC的网站是从非邮政法的内容以外初始创建不变。另外,我使用.NET Framework 4.0,因为我计划目标Azure上。

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 is unmodified from initial creation other than the contents of the Post method. Also, I'm using .NET framework 4.0 since I'm planning to target Azure.

注册方法

public static void Register(HttpConfiguration config) { config.Routes.MapHttpRoute( name: "DefaultApi", routeTemplate: "api/{controller}/{id}", defaults: new { id = RouteParameter.Optional } ); }

和Post方法

// POST api/values public string Post([FromBody]string value) { if (value != null) { return "Post was successful"; } else return "invalid post, value was null"; }

我创建它使用HttpClient的模拟张贴到该服务的控制台应用程序,但遗憾的是价值在未来向早报总是空。 POST方法成功击中下面的HttpClient上的PostAsync电话。

I created a Console application which uses HttpClient to simulate posting to the service, but unfortunately the "value" coming in to the Post is always null. The Post method is successfully hit following the PostAsync call on HttpClient.

这我不清楚如何映射我的要求,这样的价值包含我传递...

It's not clear to me how to map my request such that value contains the StringContent I'm passing in...

static void Main(string[] args) { string appendUrl = string.Format("api/values"); string totalUrl = "localhost:51744/api/values"; HttpClient client = new HttpClient(); client.DefaultRequestHeaders.Add("Accept", "application/xml"); string content = "Here is my input string"; StringContent sContent = new StringContent(content, Encoding.UTF8, "application/xml"); HttpResponseMessage response = null; string resultString = null; client.PostAsync(new Uri(totalUrl), sContent).ContinueWith(responseMessage => { response = responseMessage.Result; }).Wait(); response.Content.ReadAsStringAsync().ContinueWith(stream => { resultString = stream.Result; }).Wait(); }

我是新来的MVC的Web API和使用的HttpClient - 任何指着我在正确的方向帮助将不胜AP preciated

I'm new to the MVC web api and use of HttpClient - any help pointing me in the right direction would be greatly appreciated.

推荐答案

请尝试以下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("localhost:2451/api/values", content) .ContinueWith(task => { var response = task.Result; Console.WriteLine(response.Content.ReadAsStringAsync().Result); }); Console.ReadLine(); } }

有一个看的发送简单的类型的这篇博客文章的部分:www.asp/web-api/overview/working-with-http/sending-html-form-data,-part-1

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

更多推荐

控制台应用程序的HttpClient发布到MVC的Web API

本文发布于:2023-10-12 01:20:35,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1483352.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:控制台   应用程序   HttpClient   API   Web

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!