Отправка 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();
- }
ИИ поможет Вам:
- решить любую задачу по программированию
- объяснить код
- расставить комментарии в коде
- и т.д