How to make an HTTP POST web request

后端 未结 14 2754
有刺的猬
有刺的猬 2020-11-21 04:31

Canonical
How can I make an HTTP request and send some data using the POST method?

14条回答
  •  鱼传尺愫
    2020-11-21 04:44

    Yet another way of doing it:

    using (HttpClient httpClient = new HttpClient())
    using (MultipartFormDataContent form = new MultipartFormDataContent())
    {
        form.Add(new StringContent(param1), "param1");
        form.Add(new StringContent(param2), "param2");
        using (HttpResponseMessage response = await httpClient.PostAsync(url, form))
        {
            response.EnsureSuccessStatusCode();
            string res = await response.Content.ReadAsStringAsync();
            return res;
        }
    }
    

    This way you can easily post a stream.

提交回复
热议问题