POST请求带header头

    /// 
    /// Post请求
    /// 
    /// URL路径
    /// 请求体
    /// 头信息
    /// 
    public static string Post(string url, string postData, Dictionary head,string contentType = "application/json")
    {
        string result = string.Empty;
        using (var client = new HttpClient())
        {
            StringContent content = new StringContent(postData);
            content.Headers.ContentType = new MediaTypeHeaderValue(contentType);                
            foreach(var v in head)
            {
                content.Headers.Add(v.Key, v.Value);
            }
            var response = client.PostAsync(url, content).Result;
            if (response.IsSuccessStatusCode)
            {
                result = response.Content.ReadAsStringAsync().Result;
            }
        }
        return result;
    }

你可能感兴趣的:(POST请求带header头)