C#微信开发之旅:基础类之HttpClientHelper(更新:SSL安全策略)

包含通过httpclient发起get或post请求的方法,所有调用微信接口的操作都通过此类。话不多说,直接上代码:

2014-10-31代码更新:微信SSL安全策略调整,关闭掉SSLv2、SSLv3版本支持,不再支持部分使用SSLv2、 SSLv3或更低版本的客户端调用。

public class HttpClientHelper    {        ///         /// get请求        ///         ///         ///         public static string GetResponse(string url)        {            if (url.StartsWith("https"))                System.Net.ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls;            HttpClient httpClient = new HttpClient();            httpClient.DefaultRequestHeaders.Accept.Add(               new MediaTypeWithQualityHeaderValue("application/json"));            HttpResponseMessage response = httpClient.GetAsync(url).Result;            if (response.IsSuccessStatusCode)            {                string result = response.Content.ReadAsStringAsync().Result;                return result;            }            return null;        }        public static T GetResponse(string url)            where T : class,new()        {            if (url.StartsWith("https"))                System.Net.ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls;            HttpClient httpClient = new HttpClient();            httpClient.DefaultRequestHeaders.Accept.Add(               new MediaTypeWithQualityHeaderValue("application/json"));            HttpResponseMessage response = httpClient.GetAsync(url).Result;            T result = default(T);            if (response.IsSuccessStatusCode)            {                Task t = response.Content.ReadAsStringAsync();                string s = t.Result;                result = JsonConvert.DeserializeObject(s);            }            return result;        }        ///         /// post请求        ///         ///         /// post数据        ///         public static string PostResponse(string url, string postData)        {            if (url.StartsWith("https"))                System.Net.ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls;            HttpContent httpContent = new StringContent(postData);            httpContent.Headers.ContentType = new MediaTypeHeaderValue("application/json");            HttpClient httpClient = new HttpClient();            HttpResponseMessage response = httpClient.PostAsync(url, httpContent).Result;            if (response.IsSuccessStatusCode)            {                string result = response.Content.ReadAsStringAsync().Result;                return result;            }            return null;        }        ///         /// 发起post请求        ///         ///         /// url        /// post数据        ///         public static T PostResponse(string url, string postData)            where T : class,new()        {            if (url.StartsWith("https"))                System.Net.ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls;            HttpContent httpContent = new StringContent(postData);            httpContent.Headers.ContentType = new MediaTypeHeaderValue("application/json");            HttpClient httpClient = new HttpClient();            T result = default(T);            HttpResponseMessage response = httpClient.PostAsync(url, httpContent).Result;            if (response.IsSuccessStatusCode)            {                Task t = response.Content.ReadAsStringAsync();                string s = t.Result;                result = JsonConvert.DeserializeObject(s);            }            return result;        }        ///         /// V3接口全部为Xml形式,故有此方法        ///         ///         ///         ///         ///         public static T PostXmlResponse(string url, string xmlString)            where T : class,new()        {            if (url.StartsWith("https"))                System.Net.ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls;            HttpContent httpContent = new StringContent(xmlString);            httpContent.Headers.ContentType = new MediaTypeHeaderValue("application/json");            HttpClient httpClient = new HttpClient();            T result = default(T);            HttpResponseMessage response = httpClient.PostAsync(url, httpContent).Result;            if (response.IsSuccessStatusCode)            {                Task t = response.Content.ReadAsStringAsync();                string s = t.Result;                result = XmlDeserialize(s);            }            return result;        }        ///         /// 反序列化Xml        ///         ///         ///         ///         public static T XmlDeserialize(string xmlString)             where T : class,new ()        {            try            {                XmlSerializer ser = new XmlSerializer(typeof(T));                using (StringReader reader = new StringReader(xmlString))                {                    return (T)ser.Deserialize(reader);                }            }            catch (Exception ex)            {                throw new Exception("XmlDeserialize发生异常:xmlString:" + xmlString + "异常信息:" + ex.Message);            }        }    }

登录后复制

更多C#微信开发之旅:基础类之HttpClientHelper(更新:SSL安全策略)相关文章请关注PHP中文网!

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

发布者:PHP中文网,转转请注明出处:https://www.chuangxiangniao.com/p/1554731.html

(0)
上一篇 2025年2月18日 09:31:17
下一篇 2025年2月18日 09:31:36

AD推荐 黄金广告位招租... 更多推荐

相关推荐

  • 如何在C++中使用机器学习库来增强数据分析?

    在 c++++ 中使用机器学习库可以增强数据分析,具体步骤包括:选择适合需求的机器学习库,如 armadillo、eigen 和 tensorflow lite for microcontrollers。加载和预处理数据,选择机器学习算法,…

    2025年3月28日
    100
  • C++ 如何支持移动应用程序的离线功能

    在 c++++ 中开发离线应用程序涉及以下步骤:1. 使用 fstream 库持久化数据;2. 使用缓存机制(例如 unordered_map)存储常见数据;3. 使用异步网络请求处理在线操作。这样可以确保应用程序即使在没有互联网连接的情况…

    2025年3月28日
    100
  • c语言编译器怎么使用

    使用 C 语言编译器需要以下步骤:1. 选择编译器(如 GCC、Clang 或 Visual C++);2. 安装编译器;3. 编写 C 程序(.c 文件);4. 使用编译命令(如 gcc filename.c -o output_file…

    2025年3月28日
    100
  • c#全局变量怎么定义

    全局变量在 C# 中被定义为在整个应用程序中可访问的变量。定义语法为:[修饰符] 类型 变量名 [= 初始值],其中修饰符定义访问级别(public、internal、protected、private),类型指定数据类型,变量名遵循 C#…

    2025年3月28日
    100
  • c#程序集怎么使用

    C# 程序集是包含代码、数据和元数据的类型和资源集合。使用程序集涉及引用、创建程序集对象、获取类型、创建对象和调用方法。 C# 程序集的使用 什么是程序集? 程序集是 .NET 框架中的一组相关的类型和资源的集合。它们是独立的、可部署的单元…

    2025年3月28日
    100
  • MySQL安装时提示缺少依赖项如何解决

    #%#$#%@%@%$#%$#%#%#$%@_81c++3b080dad537de7e10e0987a4bf52e安装失败通常因缺少依赖项导致。解决方法:1. 使用系统包管理器(如linux的apt、yum或dnf,windows的visu…

    2025年3月28日
    100
  • mySQL下载完安装不了

    mysql安装失败的原因主要有:1. 权限问题,需以管理员身份运行或使用sudo命令;2. 依赖项缺失,需安装相关开发包;3. 端口冲突,需关闭占用3306端口的程序或修改配置文件;4. 安装包损坏,需重新下载并验证完整性;5. 环境变量配…

    2025年3月28日
    100
  • mysql安装失败原因

    mysql安装失败通常由以下原因导致:1. 权限问题,需管理员或root权限;2. 3306端口冲突,需检查并释放端口或修改配置;3. 依赖库缺失,需使用包管理器安装;4. 安装包损坏,需重新下载并校验;5. 环境变量问题,需正确配置安装路…

    2025年3月28日
    100
  • 连花清瘟胶囊和颗粒可以一起吃吗

    连花清瘟是目前比较热门的一种中成药,它可以起到很不错的功效与作用,可以治疗部分感冒引起的不适症状,比如流鼻涕、咳嗽以及嗓子干疼等,它有两种类型,一种是胶囊型的,另外一种是颗粒型的,这两种药效是一样的,一般是不能一起吃的。也不建议混着吃。 连…

    2025年3月27日
    51.4K00
  • 连花清瘟的连有草字头吗

    连花清瘟是目前比较常见的一种中成药,很多人家里面都会买连花清瘟。连花清瘟主要有两种,一种是连花清瘟颗粒,另外一种是连花清瘟胶囊,不管哪一种,都可以起到很不错的药用效果,并且都是不带草字头的连,主要和它的成分有关,它的主要成分是连翘和金银花。…

    2025年3月27日
    331.7K00

发表回复

登录后才能评论