【问题标题】:http request with arraylist fails使用 arraylist 的 http 请求失败
【发布时间】:2017-07-24 20:04:48
【问题描述】:

我正在执行一个 http 请求并将一个数组列表传递给一个 formUrlEncoded 属性,但这失败了

public static async Task<HttpResponseMessage> SubmitInspection(
       TruckRegistrationDetails truckdetails
        List<CommentModel> check_comments, 
        String general_comment)
{
    var body= new ArrayList(){ truckdetails, check_comments, general_comment };
    var body = new FormUrlEncodedContent(body); //this fails
    //get url from SQLite
    var response = await http.PostAsync(url, body);
    return response;
}

我得到一个错误:

无法从“System.Collections.ArrayList”转换为 'System.Collections.Generic.IEnumerable'

如何将传递的不同参数类型添加到formUrlEncoded

【问题讨论】:

  • 除了错误之外,您还需要确切地知道要传递给 PostAsync 方法的内容。它必须是接收器可以使用的格式。然后,您需要将一个或多个键值对传递给数组中的 PostAsyn 方法或实现 IEnumerable 的东西。您对需要通过的内容和格式有任何要求吗?
  • 我看到@CodingYoshi 接收器是 php,它需要一个数组或一个对象

标签: c# arraylist uwp win-universal-app


【解决方案1】:

这是FormUrlEncodedContent的构造函数的签名:

public FormUrlEncodedContent(IEnumerable<KeyValuePair<string, string>> nameValueCollection)

你看到它需要一个IEnumerable&lt;KeyValuePair&lt;string, string&gt;,但你给它一个ArrayList,所以它开始抱怨并说:

无法从“System.Collections.ArrayList”转换为“System.Collections.Generic.IEnumerable”

只需通过构建 KeyValuePair&lt;string, string&gt; 来满足它的需要。

现在您的数组列表包含TruckRegistrationDetailsList&lt;Comment&gt;String。你需要弄清楚你将从这个键值对中传递什么。

【讨论】:

    【解决方案2】:

    你应该构建Dictionary&lt;string,string&gt;

    using System.Web.Script.Serialization;
    ....
        var values = new Dictionary<string, string>();
        values.Add("truckdetails",  new JavaScriptSerializer().Serialize(truckdetails));
        values.Add("check_comments", new JavaScriptSerializer().Serialize(check_comments));
        values.Add("general_comment", general_comment);
        var body = new FormUrlEncodedContent(values);
        var response = await http.PostAsync(url, body);
        return response;
    

    别忘了添加对System.Web.Extensions的引用

    更新:

    UWP 中序列化使用JSON.Net。所以而不是:

    new JavaScriptSerializer().Serialize(truckdetails)
    

    使用:

    JsonConvert.SerializeObject(truckdetails)
    

    希望对你有帮助:)

    【讨论】:

    • System.Web.Extensions 在 uwp 中不可用
    • 因此您可以使用 JSON.NET (newtonsoft.com/json) 序列化 UWP 中的对象。
    • 您不能只将任何随机结构传递给 PostAsync 方法。它必须采用接收方可以使用的格式和结构。
    • 不是随机的!它是一个Dictionary&lt;string, string&gt;,其中包含命名值集,每个值都是有效的 json 格式或纯文本字符串。
    • 我明白了,但你怎么知道接收者期待 Json?
    【解决方案3】:

    您需要发布 FormUrlEncodedContent

    var content = new FormUrlEncodedContent(new Dictionary<string, string>
    {
        {"<your key>", "<your value>"},
        {"<your next key>", "<your next value>"},
        ...
    });
    

    然后使用您的 HttpClient 实例,发布它

    var message =
        await httpClient.PostAsync(_url, content).ConfigureAwait(false);
    

    在您的情况下,您很可能需要将您在 SubmitInspection 方法中获得的内容转换为您发布到的任何内容。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-02-28
      • 2016-09-07
      • 2014-11-19
      • 1970-01-01
      • 2016-12-17
      • 1970-01-01
      • 1970-01-01
      • 2015-02-24
      相关资源
      最近更新 更多