栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > .Net

c# .net http Post请求 发送Json数据,经过微信签名测试有效

.Net 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

c# .net http Post请求 发送Json数据,经过微信签名测试有效

c# .net http Post请求 发送Json数据

你可能还需要

c# .net 模拟请求 POST, Get,Put,Delete,并带Headers

c# .net 模拟请求 POST, Get,Put,Delete,并带Headers_橙cplvfx-技术踩坑记-CSDN博客示例Post 调用Post 调用,带Headerstring url = "http://baidu.com/api/AddUser";string postData = $"UserName=Miaoming&Age=26";Hashtable headht = new Hashtable();headht.Add("UserTokenStr", "key123456");string Res = HttpMethods.HttpPost(url, postData, head.https://blog.csdn.net/cplvfx/article/details/120687746

调用代码
//引入:using System.Collections;

Hashtable headht = new Hashtable();
headht.Add("Authorization", "内容");
HttpHelpMethods.PostJson(url, postData, headht);
  核心代码
         #region POST-JSON  
        /// 
        /// http Post请求 发送Json数据
        /// 
        /// 
        /// 
        /// 
        /// 
        public static string PostJson(string url, string postData, Hashtable headht = null)
        {
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
            request.Method = "POST";
            request.ContentType = "application/json;charset=UTF-8";
            request.UserAgent = "www.qq.cc/WebRequest";
            request.Accept = "application/json";

            #region headers设置
            if (headht != null)
            {
                foreach (DictionaryEntry item in headht)
                {
                    request.Headers.Add(item.Key.ToString(), item.Value.ToString());
                }
            }
            #endregion

            byte[] paramJsonBytes;
            paramJsonBytes = System.Text.Encoding.UTF8.GetBytes(postData);
            request.ContentLength = paramJsonBytes.Length;
            Stream writer;
            try
            {
                writer = request.GetRequestStream();
            }
            catch (Exception e)
            {
                writer = null;
                Console.Write("连接服务器失败!");
            }
            writer.Write(paramJsonBytes, 0, paramJsonBytes.Length);
            writer.Close();
            HttpWebResponse response;
            try
            {
                response = (HttpWebResponse)request.GetResponse();
            }
            catch (WebException ex)
            {
                response = ex.Response as HttpWebResponse;
            }
            Stream resStream = response.GetResponseStream();
            StreamReader reader = new StreamReader(resStream);
            string text = reader.ReadToEnd();
            return text;
        }
        #endregion 

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/911200.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号