关于http 个人见解

一、通用头部:请求头域和响应头域都支持的头域

  (1)、Cache-Control:1、no-cache; 指示请求响应都不缓存    

                 2、no-store;存应该尽快从存储器中删除文档的所有痕迹,因为其中可能会包含敏感信息。

                 3、max-age=5;浏览器5秒内会从缓存中读数据。

   (2)、Pragma:    1、Pragma:no-cache   //和上面含义一样    

  (3)、Connection    指示与服务器是否保持持久的链接  

               1、Close :否

               2、Keepalive:保持连接

               3、Keep-Alive=300:保持多少秒

  (4)、Date       代表日期

               1、代表当前时间

  (5)、Transfer-Encoding    

  (6)Accept  */*  代表任意类型

  (7)Content-Type     application/Json  标准写法  发送或请求json的标准格式

    PS:浏览器缓存问题,只有ajax的get请求会缓存post不会缓存。

二、

Content-Type :application/x-www-form-urlencoded;charset=gb2312     指示请求方以提交form表单请示传输数据,并且制定解码字符集;

private static string Decompress(Stream stream, Encoding encoding)
        {
            byte[] buffer = new byte[100];
            //int length = 0;

            using (GZipStream gz = new GZipStream(stream, CompressionMode.Decompress))
            {
                //GZipStream gzip = new GZipStream(res.GetResponseStream(), CompressionMode.Decompress);
                using (StreamReader reader = new StreamReader(gz, encoding))
                {
                    return reader.ReadToEnd();
                }
                /*
                using (MemoryStream msTemp = new MemoryStream())
                {
                    //解压时直接使用Read方法读取内容,不能调用GZipStream实例的Length等属性,否则会出错:System.NotSupportedException: 不支持此操作;
                    while ((length = gz.Read(buffer, 0, buffer.Length)) != 0)
                    {
                        msTemp.Write(buffer, 0, length);
                    }

                    return encoding.GetString(msTemp.ToArray());
                }
                 * */
            }
        }

这是解压http  response.ContentEncoding.ToLower()==gzip的方法

  private static string GetStream(HttpWebResponse response, Encoding encoding)
        {
            try
            {
                if (response.StatusCode == HttpStatusCode.OK)
                {
                    switch (response.ContentEncoding.ToLower())
                    {
                        case "gzip":
                            {
                                string result = Decompress(response.GetResponseStream(), encoding);
                                response.Close();
                                return result;
                            }
                        default:
                            {
                                using (StreamReader sr = new StreamReader(response.GetResponseStream(), encoding))
                                {
                                    string result = sr.ReadToEnd();
                                    sr.Close();
                                    sr.Dispose();
                                    response.Close();
                                    return result;
                                }
                            }
                    }
                }
                else
                {
                    response.Close();
                }
            }
            catch (Exception e)
            {
                throw e;
            }
            return "";
        }

这段是对返回的http响应流处理的方法

posted @ 2015-12-18 20:13  立于群  阅读(52)  评论(0)    收藏  举报