为什么我的 C# 客户端发布到我的 WCF REST 服务,返回 (400) 错误请求?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/575893/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-04 08:53:39  来源:igfitidea点击:

Why does my C# client, POSTing to my WCF REST service, return (400) Bad Request?

c#wcfrestpost

提问by Rob

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? :-)

我正在尝试向我编写的一个简单 WCF 服务发送 POST 请求,但我不断收到 400 Bad Request。我正在尝试将 JSON 数据发送到服务。谁能发现我做错了什么?:-)

This is my service interface:

这是我的服务接口:

public interface Itestservice
{
    [OperationContract]
    [WebInvoke(
        Method = "POST",
        UriTemplate = "/create",
        RequestFormat = WebMessageFormat.Json,
        ResponseFormat = WebMessageFormat.Json)]
    String Create(TestData testData);
}

The implementation:

实施:

public class testservice: Itestservice
{
    public String Create(TestData testData)
    {
        return "Hello, your test data is " + testData.SomeData;
    }
}

The DataContract:

数据契约:

[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();
}

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 :-(

谁能想到我可能做错了什么?正如您在 C# 客户端中看到的,我为 ContentType 尝试了 application/x-www-form-urlencoded 和 text/x-json,认为这可能与它有关,但似乎没有。我已经尝试了这个相同服务的 GET 版本,它工作正常,并且返回一个 JSON 版本的 TestData 没有问题。但是对于 POST,嗯,我现在很困惑:-(

采纳答案by Darrel Miller

Have you tried "application/json" instead of "text/x-json". According to thisStack Overflow question application/json is the only valid json media type.

您是否尝试过“application/json”而不是“text/x-json”。根据这个堆栈溢出问题 application/json 是唯一有效的 json 媒体类型。

回答by baretta

Try:

尝试:

[OperationContract]
[WebInvoke(
  Method = "POST",
  UriTemplate = "/create",
  RequestFormat = WebMessageFormat.Json,
  ResponseFormat = WebMessageFormat.Json,
  /* non-wrapped */ BodyStyle = WebMessageBodyStyle.Bare )]
String Create(TestData testData);

回答by Ray Lu

The only problem here is the ContentType.

这里唯一的问题是 ContentType。

try ( recommended )

试试(推荐)

request.ContentType = "application/json; charset=utf-8";

or ( this will work too )

或(这也行)

request.ContentType = "text/json; charset=utf-8";

Both of above solve the problem. However, the first one is recommended, for details of JSON-RPC 1.1 Specification check out http://json-rpc.org

以上两个都解决了问题。但是,建议使用第一个,有关 JSON-RPC 1.1 规范的详细信息,请查看http://json-rpc.org