无法使用C#代码在服务器上获取正文数据

时间:2018-10-05 17:39:46

标签: c# api

我正在尝试在体内发送json内容。但不幸的是,我没有在服务器中获取数据。使用邮递员工具可以接收到相同的数据。

这是我正在运行的代码

private string callAPI(string function, string content)
{
    using (var httpClient = new HttpClient())
    {
        string url = function;
        httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", txt_sessionKey.Text);
        httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/x-www-form-urlencoded"));

        httpClient.BaseAddress = new Uri(baseUrlV2);
        var json = new StringContent(content, Encoding.UTF8, "application/json");

        HttpRequestMessage request = new HttpRequestMessage
            {                    
                Method = HttpMethod.Put,
                RequestUri = new Uri(baseUrlV2+function),
                Content = json
            };
        HttpContent contentRes = httpClient.SendAsync(request).Result.Content;

        return contentRes.ReadAsStringAsync().Result;
    }
}

我在这里想念什么?

0 个答案:

没有答案