美文网首页
POST请求带header头

POST请求带header头

作者: 不语翕 | 来源:发表于2018-07-03 14:22 被阅读0次
        /// <summary>
        /// Post请求
        /// </summary>
        /// <param name="url">URL路径</param>
        /// <param name="postData">请求体</param>
        /// <param name="head">头信息</param>
        /// <returns></returns>
        public static string Post(string url, string postData, Dictionary<string, string> 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头

          本文链接:https://www.haomeiwen.com/subject/hbfnyftx.html