如何将查询字符串和 json 数据添加到 httpWebRequest 类型 POST

How to add query string and json data to httpWebRequest type POST

这里是Java代码:

function sendTextMessage(sender, text) {
  messageData = {
    text:text
  }
  request({
    url: 'https://graph.facebook.com/v2.6/me/messages',
    qs: {access_token:token},
    method: 'POST',
    json: {
      recipient: {id:sender},
      message: messageData,
    }
  }, function(error, response, body) {
    if (error) {
      console.log('Error sending message: ', error);
    } else if (response.body.error) {
      console.log('Error: ', response.body.error);
    }
  });
}

和 C# 代码,我想用代码替换 "?access_token=dshfhsfhrthytrghfgbfhnytfht",因为 url 使用类型 POST。我发现我可以使用 WebRequest 但我不知道如何使用 WebRequest 添加 Json 数据,所以我使用 HttpWebrequest,Http Webrequest 可以通过 [=19 添加查询字符串=] ,但我如何写 post 数据和 json?在这段代码中,我只写 json data

public void sendTextMessage(string sender, string text) {
            string json = "{ recipient: {id:"+sender+ "} , message: {text: \""+text+"\"}";
            var request = (HttpWebRequest)WebRequest.Create("https://graph.facebook.com/v2.6/me/messages?access_token=dshfhsfhrthytrghfgbfhnytfht");
            request.ContentType = "application/json";
            request.Method = "POST";

            using (var streamWriter = new StreamWriter(request.GetRequestStream()))
            {                
                streamWriter.Write(json);
                streamWriter.Flush();
                streamWriter.Close();
            }
            var httpResponse = (HttpWebResponse)request.GetResponse();
            using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
            {
                var result = streamReader.ReadToEnd();
            }
        }

/* 这么写 您必须将变量添加为 postdata */ var request = (HttpWebRequest)WebRequest.Create("http://www.example.com/recepticle.aspx");

var postData = "thing1=hello";
    postData += "&thing2=world";
var data = Encoding.ASCII.GetBytes(postData);

request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";
request.ContentLength = data.Length;

using (var stream = request.GetRequestStream())
{
    stream.Write(data, 0, data.Length);
}

var response = (HttpWebResponse)request.GetResponse();

var responseString = new StreamReader(response.GetResponseStream()).ReadToEnd();