C# 后台发送消息请求,构造其请求头和消息报文

   		/// <summary>
        /// 发送请求
        /// </summary>
        /// <param name="JSONData">Josn格式发送请求数据</param>
        /// <param name="token">授权返回的Token数据</param>
        /// <returns></returns>
  public static string ResponseMethod(string JSONData, string token)
        {
            string posturl = "https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token=" + token;
            string postData = JSONData;
            HttpWebRequest request = null;//http请求
            byte[] data = Encoding.UTF8.GetBytes(postData);
            string content = null;
            // 准备请求...
            try
            {
                // 设置参数
                request = WebRequest.Create(posturl) as HttpWebRequest;
                CookieContainer cookieContainer = new CookieContainer();
                request.CookieContainer = cookieContainer;
                request.AllowAutoRedirect = true;
                request.Method = "POST";//此处有Post和Get区分
                request.ContentType = "json"; //提交数据的格式有“json”和“application/x-www-form-urlencoded”区别
                request.ContentLength = data.Length;
                //设置Http发送内容
                using (Stream outstream = request.GetRequestStream())
                {
                    outstream.Write(data, 0, data.Length);
                }
                //发送Http请求,获取Http返回的数据
                using (WebResponse ResponseHttpMessage = request.GetResponse())
                {
                    if (ResponseHttpMessage != null)
                    {
                        Stream HttpResponseStream = ResponseHttpMessage.GetResponseStream();//返回数据流
                        using (StreamReader ReaderHttpResponseMessage = new StreamReader(HttpResponseStream, Encoding.UTF8))
                        {
                            content = ReaderHttpResponseMessage.ReadToEnd();
                        }
                    }
                }
            }
            catch (Exception ex)
            {

            }
            return content;
        }

1.application/x-www-form-urlencoded

在Form元素的语法中,EncType表明提交数据的格式 用 Enctype 属性指定将数据回发到服务器时浏览器使用的编码类型。 下边是说明: application/x-www-form-urlencoded: 窗体数据被编码为名称/值对。这是标准的编码格式。 multipart/form-data: 窗体数据被编码为一条消息,页上的每个控件对应消息中的一个部分。 text/plain: 窗体数据以纯文本形式进行编码,其中不含任何控件或格式字符。
补充
form 的enctype属性为编码方式,常用有两种:application/x-www-form-urlencoded和multipart/form- data,默认为application/x-www-form-urlencoded。 当action为get时候,浏览器用x-www-form-urlencoded的编码方式把form数据转换成一个字串(name1=value1& amp;amp;name2=value2…),然后把这个字串append到url后面,用?分割,加载这个新的url。 当action为post时候,浏览器把form数据封装到http body中,然后发送到server。 如果没有type=file的控件,用默认的application/x-www-form-urlencoded就可以了。 但是如果有type=file的话,就要用到multipart/form-data了。浏览器会把整个表单以控件为单位分割,并为每个部分加上 Content-Disposition(form-data或者file),Content-Type(默认为text/plain),name(控件 name)等信息,并加上分割符(boundary).

  1. application/json

有的时候发现 ajax请求中 content-type:application/json,这样也能在后台接受前台提交的数据,其实这个时候前端提交的数据是 json格式的字符串,后端要用@requestbody注解来接收

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值