为什么我的C#客户端向我的WCF REST服务POST请求会返回(400)错误请求?

18

我正在尝试向我编写的简单WCF服务发送POST请求,但我一直收到400 Bad Request的错误。我试图向服务发送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日志(客户端和/或服务器)吗? - defeated
3个回答

8
你有没有尝试使用"application/json"代替"text/x-json"?根据这个Stack Overflow问题,"application/json"是唯一合法的JSON媒体类型。请参考链接:https://dev59.com/cXRB5IYBdhLWcg3w4bKv

4
这里唯一的问题在于ContentType。
尝试使用以下代码(推荐):
request.ContentType = "application/json; charset=utf-8";

或者(这也可以工作)。
request.ContentType = "text/json; charset=utf-8";

以上两种方法都可以解决问题。然而,建议使用第一种方法,有关JSON-RPC 1.1规范的详细信息,请查看http://json-rpc.org


0

尝试:

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

它只需要一个参数。它是否被包装起来真的很重要吗?在这种情况下,服务器拒绝请求,因为请求中的contentType不被允许。 - Ray Lu
是的,contentType 也是错误的。但是我自己遇到过这个问题,所以我很确定是 BodyStyle 导致了这个问题。 - baretta

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接