Отправка POST запроса с телом JSON - C#

Узнай цену своей работы

Формулировка задачи:

Нигде не нашёл примера отправки пост запроса с телом в виде JSON, помогите, пожалуйста, вот пример JSON:
{
        "id":"11111111111111",
        "sum": {
          "amount":100,
          "currency":"643"
        },
        "paymentMethod": {
          "type":"Account",
          "accountId":"643"
        },
        "comment":"test",
        "fields": {
          "account":"+79121112233"
        }
      }'

Решение задачи: «Отправка POST запроса с телом JSON»

textual
Листинг программы
string json = @"{
        ""id"":""11111111111111"",
        ""sum"": {
          ""amount"":100,
          ""currency"":""643""
        },
        ""paymentMethod"": {
          ""type"":""Account"",
          ""accountId"":""643""
        },
        ""comment"":""test"",
        ""fields"": {
          ""account"":""+79121112233""
        }
      }'";
 
var httpRequest = (HttpWebRequest)WebRequest.Create("http://httpbin.org/post");
httpRequest.Method = "POST";
httpRequest.ContentType = "application/json";
using (var requestStream = httpRequest.GetRequestStream())
using (var writer = new StreamWriter(requestStream))
{
    writer.Write(json);
}
using (var httpResponse = httpRequest.GetResponse())
using (var responseStream = httpResponse.GetResponseStream())
using (var reader = new StreamReader(responseStream))
{
    string response = reader.ReadToEnd();
}

Оцени полезность:

14   голосов , оценка 4.071 из 5