如何在PostAsync方法中设置内容

时间:2014-06-13 17:03:40

标签: c# .net curl

调用该方法时,它返回状态代码:not found。我确信uri是正确的,所以我认为问题在于内容。这是终端呼叫的api:

curl --request POST 'https://api.uniparthenope.it/user/radius/auth' --data "user=xxxxxxxxxx" --data "passw=xxxxxxxxxx"

这是我的代码:

public async Task<string> Login()
{
    client = new HttpClient();
    client.BaseAddress = new Uri("https://api.uniparthenope.it");
    client.DefaultRequestHeaders.Accept.Clear();
    client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
    client.MaxResponseContentBufferSize = 256000;
    content = new FormUrlEncodedContent(new[]
    {
        new KeyValuePair<string,string>("user",Username),
        new KeyValuePair<string,string>("passw",Password)
    });
    content.Headers.ContentType.CharSet = "UTF-8";
    content.Headers.ContentType = new MediaTypeHeaderValue("application/x-www-form-urlencoded");
    var response = await client.PostAsync("user/radius/auth", content);
    //return response.RequestMessage.ToString();
    return response.StatusCode.ToString();
}

0 个答案:

没有答案