如何使用 HttpClient 只使用键-值参数发送 form-data POST 请求

huangapple go评论71阅读模式
英文:

How to send form-data POST request with the help HttpClient with just Key-Value parameters

问题

如何使用HttpClient仅使用键值参数发送form-data POST请求?

以下是我方法的代码:

public async Task<HttpResponseMessage> MakePostAsync(string endpoint, string token, Dictionary<string, string> headers = null, KeyValuePair<string, string>[] parameters = null)
{
    var request = new HttpRequestMessage(HttpMethod.Post, endpoint);
    if (headers != null)
    {
        foreach (var header in headers)
            request.Headers.Add(header.Key, header.Value);
    }
    request.Headers.Add("Content-Type", "multipart/form-data");
    var formData = new MultipartFormDataContent();

    formData.Add(new StringContent(token), "__RequestVerificationToken");
    formData.Add(new StringContent("admin"), "Username");
    formData.Add(new StringContent("1"), "Password");
    request.Content = formData;

    HttpResponseMessage response = await Task.Run(() => client.SendAsync(request));
    return response;
}

在JavaScript的Postan中相同的请求,它可以工作

英文:

How can I to send a form-data POST request with the help of HttpClient with just Key-Value parameters?

Here is my code of the method:

public async Task&lt;HttpResponseMessage&gt; MakePostAsync(string endpoint, string token, Dictionary&lt;string, string&gt; headers = null, KeyValuePair&lt;string, string&gt;[] parameters = null)
        {
            var request = new HttpRequestMessage(HttpMethod.Post, endpoint);
            if (headers != null)
            {
                foreach (var header in headers)
                    request.Headers.Add(header.Key, header.Value);
            }
            request.Headers.Add(&quot;Content-Type&quot;, &quot;multipart/form-data&quot;);
            var formaData = new MultipartFormDataContent();

            formaData.Add(new StringContent(token), &quot;__RequestVerificationToken&quot;);
            formaData.Add(new StringContent(&quot;admin&quot;), &quot;Username&quot;);
            formaData.Add(new StringContent(&quot;1&quot;), &quot;Password&quot;);
            request.Content = formaData;


            HttpResponseMessage response = await Task.Run(() =&gt; client.SendAsync(request));
            return response;
        }

The same request in the Postan on JavaScript, but it works

答案1

得分: 2

这个问题是通过RestSharp解决的。我需要从该请求的响应中获取Set-Cookie头的值。我只是向RestClient添加了CookieContainer。不确定如何使用HttpClient解决此问题。谢谢所有尝试帮助的人。

英文:

This problem was resolved with the help of RestSharp. I needed to get Set-Cookie header value from the response of this request. And I just added CookieContainer to the RestClient. Not sure how to manage this problem with HttpClient.
Thanks everybody who was trying to help.

答案2

得分: 0

你没有正确等待。

尝试:var response = await client.SendAsync(request);

英文:

You are not awaiting correctly.

Try: var response = await client.SendAsync(request);

huangapple
  • 本文由 发表于 2020年1月7日 01:02:26
  • 转载请务必保留本文链接:https://go.coder-hub.com/59616132.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定