控制台应用程序的HttpClient发布到MVC的Web API [英] console app HttpClient post to mvc web api

查看:289
本文介绍了控制台应用程序的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 = "http://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("http://localhost:2451/api/values", content)
            .ContinueWith(task => {

                var response = task.Result;
                Console.WriteLine(response.Content.ReadAsStringAsync().Result);
            });

        Console.ReadLine();
    }
}

有一个看的发送简单的类型的这篇博客文章的部分:<一href=\"http://www.asp.net/web-api/overview/working-with-http/sending-html-form-data,-part-1\">http://www.asp.net/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: http://www.asp.net/web-api/overview/working-with-http/sending-html-form-data,-part-1

这篇关于控制台应用程序的HttpClient发布到MVC的Web API的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆