HttpClient Get与Post请求数据

直接干货:

 

复制代码
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);
            }


复制代码

 

posted @   天天向上518  阅读(355)  评论(0编辑  收藏  举报
编辑推荐:
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· AI技术革命,工作效率10个最佳AI工具
点击右上角即可分享
微信分享提示