7.1使用Request获取HTTP请求头

首先得说说请求头包括什么吧!!这里有参考的文章,点击我跳转!!

请求头包括了:

Host    User-Agent    Accept    Accept-Language    Accept-Encoding    Connection

其实用法也就那么一个,新建字符串,用来保存这些信息,然后Request.Headers.Get(“”)填入上面的单词就可以获取了

这里直接用一个页面来做试验就算了:index.aspx.cs 代码如下:

        protected void Page_Load(object sender, EventArgs e)
        {
            string Host = Request.Headers.Get("Host");
            //定义一个字符串变量,将Request的头部,按照Host这个属性赋值给变量

            string UserAgent = Request.Headers.Get("User-Agent");
 
            string Accept = Request.Headers.Get("Accept");
 
            string AcceptLanguage = Request.Headers.Get("Accept-Language");
            //这里说一下,这边的字符串变量是没有横杠的

            string AcceptEncoding = Request.Headers.Get("Accept-Encoding");
 
            string Connection = Request.Headers.Get("Connection");
 
            Response.Write("Host="+ Host+"<br>");

            Response.Write("UserAgent=" + UserAgent + "<br>");

            Response.Write("Accept-Language=" + AcceptLanguage + "<br>");

            Response.Write("Accept-Encoding=" + AcceptEncoding + "<br>");

            Response.Write("Connection=" + Connection + "<br>");
        }

 

posted @ 2019-04-16 20:35  老乌龟  阅读(1265)  评论(0编辑  收藏  举报