带参数的POST请求

时间:2018-08-08 13:58:22

标签: c# asp.net-core

我需要发送带有正文参数的POST请求。我已经知道如何使用正文发送帖子请求,但是我不知道(如果存在)正文和正文参数之间的关系。我尝试搜索,但似乎发现的所有内容都迫使我将HttpClient替换为其他服务,但我不知道它涉及什么。

我需要在邮递员中设置两个参数:Param1:guidParam2:0

我的代码:

public async Task MarkCustomerGuid(string guid,int id)    
{
    HttpResponseMessage response;
    string json = "";
    using (var client = new HttpClient())
    {
        client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", 
            Convert.ToBase64String(
                Encoding.ASCII.GetBytes(
                     string.Format("{0}:{1}", "XX", "YY"))));
        string url = "https:XXX" + id + "@gmail.com&linkclicked1=1";

        //I'm sending an empty body for now. Should it be on the body? 
        // Or are there properties for this?
        response = await client.PostAsync(url, new StringContent(json, Encoding.UTF8, "application/json"));

        if (response.IsSuccessStatusCode)
        {
            json = await response.Content.ReadAsStringAsync();
        }
    }
}

1 个答案:

答案 0 :(得分:-1)

我不确定这是否是您的意思:

var url =  $"https://XXX{id}@gmail.com&linkclicked1=1"

var parameters = new Dictionary<string, string> { { "param1", "value" }, { "param2", "value" } };
var encodedContent = new FormUrlEncodedContent (parameters);

var response = await HttpClient.PostAsync (url, encodedContent).ConfigureAwait (false);