【问题标题】:Why does my C# client, POSTing to my WCF REST service, return (400) Bad Request?为什么我的 C# 客户端,POSTing 到我的 WCF REST 服务,返回 (400) 错误请求?
【发布时间】:2009-02-22 21:54:47
【问题描述】:

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

这是我的服务接口:

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; }
}

最后是我的客户端代码:

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,好吧,我现在对此很困惑:-(

【问题讨论】:

  • 你能提供任何http日志(客户端和/或服务器)吗?

标签: c# wcf rest post


【解决方案1】:

您是否尝试过“application/json”而不是“text/x-json”。根据thisStack Overflow 问题 application/json 是唯一有效的 json 媒体类型。

【讨论】:

    【解决方案2】:

    这里唯一的问题是 ContentType。

    试试(推荐)

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

    或者(这也可以)

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

    以上都解决了这个问题。不过还是推荐第一个,关于 JSON-RPC 1.1 规范的详细信息请查看http://json-rpc.org

    【讨论】:

      【解决方案3】:

      试试:

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

      【讨论】:

      • 它只需要一个参数。它是否被包裹真的很重要吗?在这种情况下,服务器拒绝请求,因为请求中的 contentType 不是我们想要的。
      • 是的,contentType 也是错误的。但我自己也遇到了这个问题,所以我很确定是 BodyStyle 造成的。
      猜你喜欢
      • 1970-01-01
      • 2017-08-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-09-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多