c#将多个列表传递给FormUrlEncodedContent

时间:2017-07-24 19:33:32

标签: c#

我正在执行HTTP请求,我想将多个项目传递给我的帖子请求。

public async Task<HttpResponseMessage> SubmitInspection(
                List<NewsModel> newitems, List<ChildrenModel> childs)
{

    //how do i add the above lists to one item to pass to the http body
    var values =  //stuck here = newitems and childs lists

    var body= new FormUrlEncodedContent(values);
    var response = await http.PostAsync(url, body);
    return response;
}

如何将两个List项(在上面的方法中作为参数传递)添加到我的HTTP正文?

2 个答案:

答案 0 :(得分:0)

只需声明一个数组并用它初始化{{1}}:

{{1}}

答案 1 :(得分:0)

你也可以传递这样的数据。

public async Task<HttpResponseMessage> SubmitInspection(List<object> newitems, List<object> childs)
    {
        //how do i add the above lists to one item to pass to the http body
        //stuck here = newitems and childs lists
        var myContent = Newtonsoft.Json.JsonConvert.SerializeObject(new { newitems, childs });
        var buffer = System.Text.Encoding.UTF8.GetBytes(myContent);
        var byteContent = new ByteArrayContent(buffer);
        byteContent.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("application/json");

        var response = await new HttpClient().PostAsync("", byteContent);
        return response;
    }