2

Есть класс

public struct Message
{
    public string Guid { get; set; }
    public string Token { get; set; }
}

Этот класс сериализуется в строку:

Message message = new Message { Guid = guid, Token = token };
string json = JsonConvert.SerializeObject(message);

Как ее передать в тело запроса (предполагается использовать HttpWebRequest)?

2
  • Именно HttpWebRequest нужен?
    – megacoder
    29 апр 2012 в 6:08
  • Нет, не обязательно. :) 3 мая 2012 в 11:23

1 ответ 1

2

Решение просто:

body = Encoding.UTF8.GetBytes(json);
request = (HttpWebRequest)WebRequest.Create(url);

request.Method = "POST";
request.ContentType = "application/json";
request.ContentLength = body.Length;

using (Stream stream = request.GetRequestStream())
{
    stream.Write(body, 0, body.Length);
    stream.Close();
}

using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
{
    response.Close();
}

Ваш ответ

By clicking “Отправить ответ”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Всё ещё ищете ответ? Посмотрите другие вопросы с метками или задайте свой вопрос.