c# 如何在对象不平坦时 post FormUrlEncodedContent
c# how to post FormUrlEncodedContent when object is not flat
试图 post 松懈 api 并且对如何在我的 post 中包含附件部分有疑问。我有一个看起来像
的方法
public KeyValuePair<string, string>[] GetParameters()
{
List<KeyValuePair<string, string>> parameters = new List<KeyValuePair<string, string>>();
parameters.Add(new KeyValuePair<string, string>("token", Token));
parameters.Add(new KeyValuePair<string, string>("channel", Channel));
parameters.Add(new KeyValuePair<string, string>("text", Text));
parameters.Add(new KeyValuePair<string, string>("as_user", As_User.ToString()));
return parameters.ToArray();
}
但是我的对象有一个数组,我需要为看起来像这样的附件添加数组
public class Attachment
{
public string text{get; set;}
public string pretext{ get; set;}
}
试图找出如何将附件包含在我返回的键值对数组中。或者这甚至可能不是正确的方法?
这就是我创建 post
的方式
using (HttpClient client = new HttpClient())
{
var parameters = response.GetParameters();
var requestContent = new FormUrlEncodedContent(parameters);
HttpResponseMessage r = await client.PostAsync(baseurl, requestContent);
HttpContent responseContent = r.Content;
}
假设我们谈论将消息发布到具有 chat.postMessage 的频道,附件只是请求中的另一个参数,如 token
和 channel
。因此,您可以在 GetParameters
方法中将 attachments
添加到 parameters
。
attachments
的值需要是 JSON 格式的附件数组。
因此,首先将您的 Attachment
对象转换为数组。然后使用 JavaScriptSerializer.Serialize
将其转换为 JSON 字符串
试图 post 松懈 api 并且对如何在我的 post 中包含附件部分有疑问。我有一个看起来像
的方法 public KeyValuePair<string, string>[] GetParameters()
{
List<KeyValuePair<string, string>> parameters = new List<KeyValuePair<string, string>>();
parameters.Add(new KeyValuePair<string, string>("token", Token));
parameters.Add(new KeyValuePair<string, string>("channel", Channel));
parameters.Add(new KeyValuePair<string, string>("text", Text));
parameters.Add(new KeyValuePair<string, string>("as_user", As_User.ToString()));
return parameters.ToArray();
}
但是我的对象有一个数组,我需要为看起来像这样的附件添加数组
public class Attachment
{
public string text{get; set;}
public string pretext{ get; set;}
}
试图找出如何将附件包含在我返回的键值对数组中。或者这甚至可能不是正确的方法?
这就是我创建 post
的方式using (HttpClient client = new HttpClient())
{
var parameters = response.GetParameters();
var requestContent = new FormUrlEncodedContent(parameters);
HttpResponseMessage r = await client.PostAsync(baseurl, requestContent);
HttpContent responseContent = r.Content;
}
假设我们谈论将消息发布到具有 chat.postMessage 的频道,附件只是请求中的另一个参数,如 token
和 channel
。因此,您可以在 GetParameters
方法中将 attachments
添加到 parameters
。
attachments
的值需要是 JSON 格式的附件数组。
因此,首先将您的 Attachment
对象转换为数组。然后使用 JavaScriptSerializer.Serialize