为什么我的 C# 客户端发布到我的 WCF REST 服务,返回 (400) 错误请求? [英] Why does my C# client, POSTing to my WCF REST service, return (400) Bad Request?
问题描述
我正在尝试向我编写的一个简单 WCF 服务发送 POST 请求,但我不断收到 400 错误请求.我正在尝试将 JSON 数据发送到服务.谁能发现我做错了什么?:-)
I'm trying to send a POST request to a simple WCF service I wrote, but I keep getting a 400 Bad Request. I'm trying to send JSON data to the service. Can anyone spot what I'm doing wrong? :-)
这是我的服务接口:
public interface Itestservice
{
[OperationContract]
[WebInvoke(
Method = "POST",
UriTemplate = "/create",
RequestFormat = WebMessageFormat.Json,
ResponseFormat = WebMessageFormat.Json)]
String Create(TestData testData);
}
实现:
public class testservice: Itestservice
{
public String Create(TestData testData)
{
return "Hello, your test data is " + testData.SomeData;
}
}
数据契约:
[DataContract]
public class TestData
{
[DataMember]
public String SomeData { get; set; }
}
最后是我的客户端代码:
And finally my client code:
private static void TestCreatePost()
{
Console.WriteLine("testservice.svc/create POST:");
Console.WriteLine("-----------------------");
Uri address = new Uri("http://localhost:" + PORT + "/testweb/testservice.svc/create");
// Create the web request
HttpWebRequest request = WebRequest.Create(address) as HttpWebRequest;
// Set type to POST
request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";
//request.ContentType = "text/x-json";
// Create the data we want to send
string data = "{"SomeData":"someTestData"}";
// Create a byte array of the data we want to send
byte[] byteData = UTF8Encoding.UTF8.GetBytes(data);
// Set the content length in the request headers
request.ContentLength = byteData.Length;
// Write data
using (Stream postStream = request.GetRequestStream())
{
postStream.Write(byteData, 0, byteData.Length);
}
// Get response
using (HttpWebResponse response = request.GetResponse() as HttpWebResponse)
{
// Get the response stream
StreamReader reader = new StreamReader(response.GetResponseStream());
// Console application output
Console.WriteLine(reader.ReadToEnd());
}
Console.WriteLine();
Console.WriteLine();
}
谁能想到我可能做错了什么?正如您在 C# 客户端中看到的,我为 ContentType 尝试了 application/x-www-form-urlencoded 和 text/x-json,认为这可能与它有关,但似乎没有.我已经尝试了这个相同服务的 GET 版本,它工作正常,并且返回一个 JSON 版本的 TestData 没有问题.但是对于 POST,嗯,我现在很困惑:-(
Can anyone think of what I might be doing wrong? As you can see in the C# client I've tried both application/x-www-form-urlencoded and text/x-json for ContentType, thinking that might have something to do with it, but it doesn't seem to. I've tried a GET version of this same service and it works fine, and returns a JSON version of TestData with no problem. But for POST, well, I'm pretty stuck at the moment on this :-(
推荐答案
您是否尝试过application/json"而不是text/x-json".根据 this Stack Overflow question application/json 是唯一有效的 json 媒体类型.
Have you tried "application/json" instead of "text/x-json". According to this Stack Overflow question application/json is the only valid json media type.
这篇关于为什么我的 C# 客户端发布到我的 WCF REST 服务,返回 (400) 错误请求?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!