2

メッセージを uri に投稿するメソッドを作成しました。

public string RestClientPost(string uri, string message = null)
    {
        var client = new RestClient(uri);
        var request = new RestRequest(Method.POST);
        request.AddHeader("Accept", "text/xml");
        if (!string.IsNullOrEmpty(message))
            request.AddParameter(message, ParameterType.RequestBody);

        var result = "";
        var response = client.Execute(request);

        if (response.StatusCode == HttpStatusCode.OK)
        {
            result = response.Content;
            Console.WriteLine(result);
        }
        else
        {
            result = response.StatusCode.ToString();
        }


        return result;
    }

以下のコードは、上記の方法で投稿するために使用されます。

public void test123()
    {
        string uri = "myuri"; //private uri, cannot expose.
        var file= System.IO.File.ReadAllText(Path.Combine(Settings.EnvValPath, "RestClientXML", "test.XML"));
        var content = new RestClientServices().RestClientPost(uri, file);

    }

ただし、「サポートされていないメディア タイプ」が返されます。

私のtest.XMLの内容は

<customer> 
    <customerName>test</customerName > 
    <customerStatus>OK</customerStatus > 
</customer>

また、Google Chrome 用の Advanced Rest Client Plugin を使用すると、投稿して、必要な文字列を返すことができます。何か問題がありますか?? Advanced Rest Client で「content-type」を「text/xml」に設定しました。

  • 返信メッセージは顧客の ID です。例: 2132
4

2 に答える 2

0

これは、ヘッダー「Accept」が戻りオブジェクトのタイプを指定するためです。この場合、送信するコンテンツのタイプではなく、変数contentの値です。送信するコンテンツのタイプを指定します: "Content-Type: application/xml"。

POST リクエストのリターン タイプがメディア ファイルの場合は、'image/png' または 'image/jpeg' を使用できます。「 application/xml 」、「 application/xhtml+xml 」、「 image/png 」など、複数の Accept ヘッダー値を使用できます。たとえば、Fiddlerを使用して HTTP(s) トラフィックをデバッグできます。これは Web 開発者にとって優れたツールです。

于 2013-07-17T04:01:22.707 に答える