HttpClient Get与Post请求数据

导读:本篇文章讲解 HttpClient Get与Post请求数据,希望对大家有帮助,欢迎收藏,转发!站点地址:www.bmabk.com

直接干货:

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Threading.Tasks;

namespace FEG.ESB.Common.Helper
{
    /// <summary>
    /// 
    /// </summary>
    public class HttpClientHelper
    {
        private static readonly HttpClient httpClient = new HttpClient();


        /// <summary>
        /// Post 请求
        /// </summary>
        /// <param name="url"></param>
        /// <param name="dic"></param>
        /// <returns></returns>
        public static string PostString(string url, Dictionary<string, string> dic)
        {
            string responseString = string.Empty;
            try
            {
                var content = new FormUrlEncodedContent(dic);
                var response = httpClient.PostAsync(url, content).Result;
                responseString = response.Content.ReadAsStringAsync().Result;
            }
            catch (Exception)
            {
            }
            return responseString;
        }

        // <summary>
        /// post提交并反馈结果
        /// </summary>
        /// <param name="url"></param>
        /// <param name="xmlString"></param>
        /// <returns></returns>
        public static string PostXmlResponse(string url, string xmlString)
        {
            HttpContent httpContent = new StringContent(xmlString);
            httpContent.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("application/josn");
            HttpResponseMessage res = httpClient.PostAsync(url, httpContent).Result;
            if (res.IsSuccessStatusCode)
            {
                Task<string> t = res.Content.ReadAsStringAsync();
                return t.Result;
            }
            return string.Empty;
        }
        /// <summary>
        /// Get请求数据
        /// </summary>
        /// <param name="url"></param>
        /// <returns></returns>
        public static string GetResponse(string url)
        {
            HttpResponseMessage res = httpClient.GetAsync(url).Result;
            if (res.IsSuccessStatusCode)
            {
                Task<string> t = res.Content.ReadAsStringAsync();
                return t.Result;
            }
            return string.Empty;
        }
    }
}

 

 

Post 或者如下调用:


//--HttpClientHelper.PostString(url,values);
      try
            {
                System.Net.Http.HttpClient httpClient = new System.Net.Http.HttpClient();
                var values = new Dictionary<string, string>
                {
                    { "cou_id", "A9E52C76-0CE7-4261-54C3-7EB04062F758" }
                };

                var content = new System.Net.Http.FormUrlEncodedContent(values);
                var response = httpClient.PostAsync("http://localhost:8888/Api/Course/GetCourseStudent", content).Result;
                var responseString = response.Content.ReadAsStringAsync().Result;
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }


 

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/64004.html

(0)
小半的头像小半

相关推荐

极客之音——专业性很强的中文编程技术网站,欢迎收藏到浏览器,订阅我们!