2009-02-22 8 views
17

私が書いたシンプルなWCFサービスにPOSTリクエストを送信しようとしていますが、私は400 Bad Requestを取得し続けています。 JSONデータをサービスに送信しようとしています。誰でも私が間違っていることを見つけることができますか? :-)なぜ私のC#クライアントがWCF RESTサービスにPOSTすると、Bad Requestが返されますか?

これは私のサービス・インターフェースである:

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:

[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バージョンを試してみましたがうまくいき、問題のないTestDataのJSONバージョンを返します。しかし、POSTのために、よく、私はこの:-(

+0

HTTPログ(クライアントおよび/またはサーバー)を提供できますか? – defeated

答えて

8

あなたの代わりに "テキスト/ X-JSON" の "アプリケーション/ JSONを" 試してみました。 thisスタックオーバーフロー質問アプリケーション/ JSONによるのみ有効なJSONメディアタイプである。

+0

これは、ありがとう:-) –

0

してください上の瞬間にはかなりこだわっている:

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

には1つのパラメータしかかかりません。包まれているかどうかは本当に重要ですか?この場合、要求内のcontentTypeが望ましくないため、サーバーは要求を拒否します。 –

+0

確かにcontentTypeも間違っています。しかし、私は自分自身でこの問題を抱えていたので、これを引き起こしているBodyStyleだとはかなり確信していました。 – baretta

4

唯一の問題はここにContentTypeをである。

試し(推奨)

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

又は(これも動作する)

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

上記の両方の問題を解決します。しかし、最初のものが推奨されています。JSON-RPC 1.1仕様チェックアウトの詳細についてはhttp://json-rpc.org

関連する問題