zoukankan      html  css  js  c++  java
  • application/json 和 application/x-www-form-urlencoded的区别

        public static string HttpPost(string url, string body)
        {
            //ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(CheckValidationResult);
            Encoding encoding = Encoding.UTF8;
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
            request.Method = "POST";
            request.Accept = "text/html, application/xhtml+xml, */*";
            request.ContentType = "application/x-www-form-urlencoded";//以键值对形式key1=value1&key2=value2的方式发送到服务器
            //request.ContentType = "application/json";//以标准的json格式的方式发送到服务器
    
    
            byte[] buffer = encoding.GetBytes(body);
            request.ContentLength = buffer.Length;
            request.GetRequestStream().Write(buffer, 0, buffer.Length);
            HttpWebResponse response = (HttpWebResponse)request.GetResponse();
            using (StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8))
            {
                return reader.ReadToEnd();
            }
        }

    application/x-www-form-urlencoded方式是以键值对形式key1=value1&key2=value2的方式发送到服务器,这种方式的好处就是浏览器都支持,在请求发送过程中会对数据进行序列化处理。
    application/json方式是以标准的json格式的方式发送到服务器,,随着json规范的越来越流行,并且浏览器支持程度原来越好,许多开发人员易application/json作为请求content-type,告诉服务器请求的主题内容是json格式的字符串,服务器端会对json字符串进行解析,这种方式的好处就是前端人员不需要关心数据结构的复杂度,只要是标准的json格式就能提交成功,application/json数据格式越来越得到开发人员的青睐。

  • 相关阅读:
    无向图判断三元环
    POJ 2785 4 Values whose Sum is 0
    lower_bound和upper_bound
    2153: 2018湖南多校第二场-20180407(网络同步赛)
    前缀和、前缀积
    hdu 4686 Arc of Dream
    UVA Recurrences 矩阵相乘+快速幂
    UVA 11149 Power of Matrix 构造矩阵
    poj 1258 Agri-Net prim模板 prim与dijkstra的区别
    poj 1182 食物链 (并查集)
  • 原文地址:https://www.cnblogs.com/cuihongyu3503319/p/9171952.html
Copyright © 2011-2022 走看看