私が使用しているSystem.Net.Http、私はウェブ上でいくつかの例を見つけました。私はPOSTリクエストを行うためにこのコードを作成することができました:
public static string POST(string resource, string token)
{
using (var client = new HttpClient())
{
client.BaseAddress = new Uri(baseUri);
client.DefaultRequestHeaders.Add("token", token);
var content = new FormUrlEncodedContent(new[]
{
new KeyValuePair<string, string>("", "")
});
var result = client.PostAsync("", content).Result;
string resultContent = result.Content.ReadAsStringAsync().Result;
return resultContent;
}
}
すべて正常に動作しています。しかし、POSTメソッドに3番目のパラメータであるdata。というパラメータを渡したいとします。データパラメータは次のようなオブジェクトです。
object data = new
{
name = "Foo",
category = "article"
};
作成せずにそれを行うにはどうすればよいKeyValuePairですか?私のphpRestAPIはjson入力を待つので、FormUrlEncodedContentはrawjsonを正しく送信する必要があります。しかし、どうすればこれを行うことができMicrosoft.Net.Httpますか?ありがとう。