为什么我的HttpWebRequest POST方法到我的WebAPI服务器失败? [英] Why is my HttpWebRequest POST method to my WebAPI server failing?

查看:178
本文介绍了为什么我的HttpWebRequest POST方法到我的WebAPI服务器失败?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经成功地从我的WebAPI项目(GET)接收到的数据,但我的尝试后无法正常工作。下面是相关的服务器/的WebAPI code:

I've successfully received data from my WebAPI project ("GET"), but my attempt to Post is not working. Here is the relevant server/WebAPI code:

public Department Add(Department item)
{
    if (item == null)
    {
        throw new ArgumentNullException("item");
    }
    departments.Add(item);
    return item; 
}

而失败的......departments.Add(项目);线,当来自客户端的这个code调用:

...which fails on the "departments.Add(item);" line, when this code from the client is invoked:

const string uri = "http://localhost:48614/api/departments";
var dept = new Department();
dept.Id = 8;
dept.AccountId = "99";
dept.DeptName = "Something exceedingly funky";

var webRequest = (HttpWebRequest)WebRequest.Create(uri);
webRequest.Method = "POST";
var deptSerialized = JsonConvert.SerializeObject(dept); // <-- This is JSON.NET; it works (deptSerialized has the JSONized versiono of the Department object created above)
using (StreamWriter sw = new StreamWriter(webRequest.GetRequestStream()))
{
    sw.Write(deptSerialized);
}
HttpWebResponse httpWebResponse = webRequest.GetResponse() as HttpWebResponse;
using (StreamReader sr = new StreamReader(httpWebResponse.GetResponseStream()))
{
    if (httpWebResponse.StatusCode != HttpStatusCode.OK)
    {
        string message = String.Format("POST failed. Received HTTP {0}", httpWebResponse.StatusCode);
        throw new ApplicationException(message);
    }  
    MessageBox.Show(sr.ReadToEnd());
}

...它失败的HttpWebResponse httpWebResponse = webRequest.GetResponse()作为HttpWebResponse;行。

...which fails on the "HttpWebResponse httpWebResponse = webRequest.GetResponse() as HttpWebResponse;" line.

在服务器上的错误消息是,有关部门为null; deptSerialized被填充了JSON记录,所以......缺什么吗?

The err msg on the server is that departments is null; deptSerialized is being populated with the JSON "record" so...what is missing here?

指定的ContentType没有,确实解决了难题。此外,状态code为创建,使得code以上抛出一个异常,所以我把它改为:

Specifying the ContentType did, indeed, solve the dilemma. Also, the StatusCode is "Created", making the code above throw an exception, so I changed it to:

using (StreamReader sr = new StreamReader(httpWebResponse.GetResponseStream()))
{
    MessageBox.Show(String.Format("StatusCode == {0}", httpWebResponse.StatusCode));
    MessageBox.Show(sr.ReadToEnd());
}

...这表明状态code ==创建后面的JSON记录(数组成员?任期。?)我创建的。

...which shows "StatusCode == Created" followed by the JSON "record" (array member? term.?) I created.

推荐答案

您忘了设置正确的内容类型请求头:

You forgot to set the proper Content-Type request header:

webRequest.ContentType = "application/json";

您写在你的POST请求的身体有些JSON的有效载荷,但你怎么能指望在Web API服务器知道你发送JSON的有效载荷,而不是XML或其他什么东西?您需要设置为此事正确的Content-Type请求头。

You wrote some JSON payload in the body of your POST request but how do you expect the Web API server to know that you sent JSON payload and not XML or something else? You need to set the proper Content-Type request header for that matter.

这篇关于为什么我的HttpWebRequest POST方法到我的WebAPI服务器失败?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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