2016-11-18 55 views
1

试图张贴到松散API,并有关于如何在我的文章中包含附件部分的问题。我有一个方法,它看起来像c#如何发布FormUrlEncodedContent时,对象不平坦

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;} 
} 

试图找出如何将在keyvaluepair附件我在这里返回的数组。或者,这可能不是正确的方法来做到这一点?

为了什么它的价值,这是我如何创建后

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; 
      } 

回答

1

假设我们谈论发布的消息将频道与chat.postMessage附件是像tokenchannel请求只是一个参数。因此,您可以在GetParameters方法中将attachments添加到parameters中。

attachments的值需要是JSON格式的附件数组。

因此,首先将您的Attachment对象转换为数组。然后将其转换成JSON字符串使用JavaScriptSerializer.Serialize

+0

是的谈论chat.postMessage。 –