112

RestSharpを使用してWebサービスを利用しようとしています。これまでのところ、すべてが順調に進んでいます(John Sheehanとすべての貢献者に乾杯!)が、私は問題にぶつかりました。すでにシリアル化された形式で(つまり、文字列として)XMLをRestRequestの本文に挿入したいとします。これを行う簡単な方法はありますか?.AddBody()関数が舞台裏でシリアル化を実行しているように見えるので、私の文字列はに変換されてい<String />ます。

どんな助けでも大歓迎です!

編集:私の現在のコードのサンプルが要求されました。下記参照 -

private T ExecuteRequest<T>(string resource,
                            RestSharp.Method httpMethod,
                            IEnumerable<Parameter> parameters = null,
                            string body = null) where T : new()
{
    RestClient client = new RestClient(this.BaseURL);
    RestRequest req = new RestRequest(resource, httpMethod);

    // Add all parameters (and body, if applicable) to the request
    req.AddParameter("api_key", this.APIKey);
    if (parameters != null)
    {
        foreach (Parameter p in parameters) req.AddParameter(p);
    }

    if (!string.IsNullOrEmpty(body)) req.AddBody(body); // <-- ISSUE HERE

    RestResponse<T> resp = client.Execute<T>(req);
    return resp.Data;
}
4

2 に答える 2

231

プレーンなxml文字列をリクエスト本文に追加する方法は次のとおりです。

req.AddParameter("text/xml", body, ParameterType.RequestBody);

于 2011-03-22T15:28:07.340 に答える
7

@dmitreygの回答に追加し、@ jrahhaliの回答に対するコメントごとに、現在のバージョンでは、これが投稿された時点v105.2.3で、構文は次のようになっています。

request.Parameters.Add(new Parameter() { 
    ContentType = "application/json", 
    Name = "JSONPAYLOAD", // not required 
    Type = ParameterType.RequestBody, 
    Value = jsonBody
});

request.Parameters.Add(new Parameter() { 
    ContentType = "text/xml", 
    Name = "XMLPAYLOAD", // not required 
    Type = ParameterType.RequestBody, 
    Value = xmlBody
});
于 2017-06-29T20:14:14.237 に答える