为什么这个帖子操作失败? [英] Why is this Post operation failing?

查看:138
本文介绍了为什么这个帖子操作失败?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

根据这个,我的网站API项目,我使用的客户端上的这code:

Based on this, for my Web API project, I'm using this code on the client:

private void AddDepartment()
{
    int onAccountOfWally = 42;
    string moniker = "Billy Bob";
    Cursor.Current = Cursors.WaitCursor;
    try
    {
        string uri = String.Format("http://platypi:28642/api/Departments/{0}/{1}", onAccountOfWally, moniker);
        var webRequest = (HttpWebRequest)WebRequest.Create(uri);
        webRequest.Method = "POST";
        var webResponse = (HttpWebResponse)webRequest.GetResponse();
        if (webResponse.StatusCode != HttpStatusCode.OK)
        {
            MessageBox.Show(string.Format("Failed: {0}", webResponse.StatusCode.ToString()));
        }
    }
    finally
    {
        Cursor.Current = Cursors.Default;
    }
}

我达到我在这一行code的设置断点:

I reach the breakpoint I set on this line of code:

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

...但是当我F10在它(或尝试的以F11为它),它失败的远程服务器返回错误(411)需要长度

...but when I F10 over it (or try to F11 into it), it fails with "The remote server returned an error (411) Length Required"

中的什么的要求,Compilerobot?!?

The length of what is required, Compilerobot?!?

这是我在服务器的存储库类方法:

This is my method in the server's Repository class:

public void Post(Department department)
{
    int maxId = departments.Max(d => d.Id);
    department.Id = maxId + 1;
    departments.Add(department);
}

控制器code是:

The Controller code is:

public void Post(Department department)
{
    deptsRepository.Post(department);
}

我的GET方法做工精细; POST是下一个步骤,但我碰伤我的脚趾至今。

My GET methods are working fine; POST is the next step, but I'm stubbing my toe so far.

推荐答案

你没有发布任何东西。

当你..你需要提供内容的长度。有点像这样的:

When you do.. you need to supply the length of the content. Somewhat like this:

byte[] yourData = new byte[1024]; // example only .. this will be your data

webRequest.ContentLength = yourData.Length; // set Content Length

var requestStream = webRequest.GetRequestStream(); // get stream for request

requestStream.Write(yourData, 0, yourData.Length); // write to request stream

这篇关于为什么这个帖子操作失败?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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