[ASP.net Web Form] 查看Request.ServerVariables資訊

[ASP.net Web Form] 查看Request資訊

最近論壇上有人發問

1.怎麼判斷使用者是不是按F5重新整理Postback(重覆交易問題)

2.如何判斷是否是xmlhttpRequest請求

因為自己也有此需求,所以紀錄一下觀察結果

(要用HttpWatch軟體看也是一種選擇,但看得到總該想辦法取到值來判斷)

 

測試環境:Windows 7 家用進階版 + IE8瀏覽器 + 把Web Site掛到本機的IIS7.5(應用程式集區ASP.NET v4.0 整合式)

看表格時,須搭配MSDN文件HttpRequest.ServerVariables屬性IIS Server Variables瞭解各Header Key值代表什麼意思

(簡體中文版:HttpRequest.ServerVariables[]中的参数集 - 花香的蜂 - 博客园)

 

程式碼:

.aspx


<%@ Page Language="C#" AutoEventWireup="true" CodeFile="HttpRequestWatcher.aspx.cs" Inherits="HttpRequestWatcher" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <script src="js/jquery-1.4.1.js" type="text/javascript"></script>
    <script type="text/javascript">
        function jQueryGet() {
            $.get("HttpRequestWatcher.aspx", { data: "passQueryString" }, jqueryCallHandler);
        }

        function jQueryPost() {
            $.post("HttpRequestWatcher.aspx", { data: "passFormData" }, jqueryCallHandler);
        }

        function jqueryCallHandler(htmlVal) {
            $("#div_jQueryAjax").html(htmlVal);
        }
    
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <asp:ScriptManager runat="server" /><br />

    Page_Load回傳的字串:<br />
    <asp:Literal id="li_pageLoadText" runat="server" /><br />
    <asp:Button Text="按鈕PostBack" ID="btn_Go" runat="server" /><br />
    <hr />
    jQuery區:<br />
    <a id="a_jQueryGet" href="javascript:jQueryGet();" >jQueryAjaxGet</a><br />
    <a id="a_jQueryPost"  href="javascript:jQueryPost();" >jQueryAjaxPost</a><br />
    jQuery回傳的HTML:<br />
    <div id="div_jQueryAjax">
    </div>
    <hr />
    
    ASP.net UpdatePanel區:<br />
    <asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional">
        <ContentTemplate>
            <asp:Button Text="按鈕Updatepanel" ID="btn_UpdatePanel" runat="server" 
                onclick="btn_UpdatePanel_Click" /><br />
                <asp:Literal ID="li_Text"  runat="server" /><br />
        </ContentTemplate>
        <Triggers>
            <asp:AsyncPostBackTrigger ControlID="btn_UpdatePanel" EventName="Click" />
        </Triggers>
    </asp:UpdatePanel>
    </form>
</body>
</html>

.cs


using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;

public partial class HttpRequestWatcher : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        StringBuilder sb = new StringBuilder();
        sb.Append("<table width='100%' border='1'  cellpadding='0' cellspacing='0'>");
        foreach (string key in Request.ServerVariables.AllKeys)
        {
            sb.Append("<tr>");
            sb.Append("<td>" + key + "</td>" + "<td>" + Request.ServerVariables[key] + "</td>");
            sb.Append("</tr>");
        }
        sb.Append("</table>");

        li_pageLoadText.Text = sb.ToString();
    }
    protected void btn_UpdatePanel_Click(object sender, EventArgs e)
    {
        StringBuilder sb = new StringBuilder();
        sb.Append("<table width='100%' border='1'  cellpadding='0' cellspacing='0'>");
        foreach (string key in Request.ServerVariables.AllKeys)
        {
            sb.Append("<tr>");

            sb.Append("<td>" + key + "</td>" + "<td>" + Request.ServerVariables[key] + "</td>");
            sb.Append("</tr>");
        }
        sb.Append("</table>");

        li_Text.Text = sb.ToString();
    }
}

 

發出的要求網址皆為:http://localhost/MyHttpWatch/HttpRequestWatcher.aspx

Case 1:網頁第一次執行(Get Method)的畫面:

 
 
 

Page_Load回傳的字串(對照組):
ALL_HTTP HTTP_CONNECTION:Keep-Alive HTTP_ACCEPT:application/x-ms-application, image/jpeg, application/xaml+xml, image/gif, image/pjpeg, application/x-ms-xbap, application/x-shockwave-flash, application/vnd.ms-excel, */* HTTP_ACCEPT_ENCODING:gzip, deflate HTTP_ACCEPT_LANGUAGE:zh-tw HTTP_HOST:localhost HTTP_USER_AGENT:Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; WOW64; Trident/4.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; .NET4.0C; .NET4.0E; MASP)
ALL_RAW Connection: Keep-Alive Accept: application/x-ms-application, image/jpeg, application/xaml+xml, image/gif, image/pjpeg, application/x-ms-xbap, application/x-shockwave-flash, application/vnd.ms-excel, */* Accept-Encoding: gzip, deflate Accept-Language: zh-tw Host: localhost User-Agent: Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; WOW64; Trident/4.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; .NET4.0C; .NET4.0E; MASP)
APPL_MD_PATH /LM/W3SVC/1/ROOT/MyHttpWatch
APPL_PHYSICAL_PATH D:\MyHttpWatch\
AUTH_TYPE  
AUTH_USER  
AUTH_PASSWORD  
LOGON_USER  
REMOTE_USER  
CERT_COOKIE  
CERT_FLAGS  
CERT_ISSUER  
CERT_KEYSIZE  
CERT_SECRETKEYSIZE  
CERT_SERIALNUMBER  
CERT_SERVER_ISSUER  
CERT_SERVER_SUBJECT  
CERT_SUBJECT  
CONTENT_LENGTH 0
CONTENT_TYPE  
GATEWAY_INTERFACE CGI/1.1
HTTPS off
HTTPS_KEYSIZE  
HTTPS_SECRETKEYSIZE  
HTTPS_SERVER_ISSUER  
HTTPS_SERVER_SUBJECT  
INSTANCE_ID 1
INSTANCE_META_PATH /LM/W3SVC/1
LOCAL_ADDR ::1
PATH_INFO /MyHttpWatch/HttpRequestWatcher.aspx
PATH_TRANSLATED D:\MyHttpWatch\HttpRequestWatcher.aspx
QUERY_STRING  
REMOTE_ADDR ::1
REMOTE_HOST ::1
REMOTE_PORT 4483
REQUEST_METHOD GET
SCRIPT_NAME /MyHttpWatch/HttpRequestWatcher.aspx
SERVER_NAME localhost
SERVER_PORT 80
SERVER_PORT_SECURE 0
SERVER_PROTOCOL HTTP/1.1
SERVER_SOFTWARE Microsoft-IIS/7.5
URL /MyHttpWatch/HttpRequestWatcher.aspx
HTTP_CONNECTION Keep-Alive
HTTP_ACCEPT application/x-ms-application, image/jpeg, application/xaml+xml, image/gif, image/pjpeg, application/x-ms-xbap, application/x-shockwave-flash, application/vnd.ms-excel, */*
HTTP_ACCEPT_ENCODING gzip, deflate
HTTP_ACCEPT_LANGUAGE zh-tw
HTTP_HOST localhost
HTTP_USER_AGENT Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; WOW64; Trident/4.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; .NET4.0C; .NET4.0E; MASP)

 
 

 

Case 2:按下畫面上按鈕PostBack(Post Method)畫面

Page_Load回傳的字串

ALL_HTTP HTTP_CACHE_CONTROL:no-cache HTTP_CONNECTION:Keep-Alive HTTP_CONTENT_LENGTH:5377 HTTP_CONTENT_TYPE:application/x-www-form-urlencoded HTTP_ACCEPT:application/x-ms-application, image/jpeg, application/xaml+xml, image/gif, image/pjpeg, application/x-ms-xbap, application/x-shockwave-flash, application/vnd.ms-excel, */* HTTP_ACCEPT_ENCODING:gzip, deflate HTTP_ACCEPT_LANGUAGE:zh-TW HTTP_HOST:localhost HTTP_REFERER:http://localhost/MyHttpWatch/HttpRequestWatcher.aspx HTTP_USER_AGENT:Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; WOW64; Trident/4.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; .NET4.0C; .NET4.0E; MASP)
ALL_RAW Cache-Control: no-cache Connection: Keep-Alive Content-Length: 5377 Content-Type: application/x-www-form-urlencoded Accept: application/x-ms-application, image/jpeg, application/xaml+xml, image/gif, image/pjpeg, application/x-ms-xbap, application/x-shockwave-flash, application/vnd.ms-excel, */* Accept-Encoding: gzip, deflate Accept-Language: zh-TW Host: localhost Referer: http://localhost/MyHttpWatch/HttpRequestWatcher.aspx User-Agent: Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; WOW64; Trident/4.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; .NET4.0C; .NET4.0E; MASP)
APPL_MD_PATH /LM/W3SVC/1/ROOT/MyHttpWatch
APPL_PHYSICAL_PATH D:\MyHttpWatch\
AUTH_TYPE  
AUTH_USER  
AUTH_PASSWORD  
LOGON_USER  
REMOTE_USER  
CERT_COOKIE  
CERT_FLAGS  
CERT_ISSUER  
CERT_KEYSIZE  
CERT_SECRETKEYSIZE  
CERT_SERIALNUMBER  
CERT_SERVER_ISSUER  
CERT_SERVER_SUBJECT  
CERT_SUBJECT  
CONTENT_LENGTH 5377
CONTENT_TYPE application/x-www-form-urlencoded
GATEWAY_INTERFACE CGI/1.1
HTTPS off
HTTPS_KEYSIZE  
HTTPS_SECRETKEYSIZE  
HTTPS_SERVER_ISSUER  
HTTPS_SERVER_SUBJECT  
INSTANCE_ID 1
INSTANCE_META_PATH /LM/W3SVC/1
LOCAL_ADDR ::1
PATH_INFO /MyHttpWatch/HttpRequestWatcher.aspx
PATH_TRANSLATED D:\MyHttpWatch\HttpRequestWatcher.aspx
QUERY_STRING  
REMOTE_ADDR ::1
REMOTE_HOST ::1
REMOTE_PORT 4488
REQUEST_METHOD POST
SCRIPT_NAME /MyHttpWatch/HttpRequestWatcher.aspx
SERVER_NAME localhost
SERVER_PORT 80
SERVER_PORT_SECURE 0
SERVER_PROTOCOL HTTP/1.1
SERVER_SOFTWARE Microsoft-IIS/7.5
URL /MyHttpWatch/HttpRequestWatcher.aspx
HTTP_CACHE_CONTROL no-cache
HTTP_CONNECTION Keep-Alive
HTTP_CONTENT_LENGTH 5377
HTTP_CONTENT_TYPE application/x-www-form-urlencoded
HTTP_ACCEPT application/x-ms-application, image/jpeg, application/xaml+xml, image/gif, image/pjpeg, application/x-ms-xbap, application/x-shockwave-flash, application/vnd.ms-excel, */*
HTTP_ACCEPT_ENCODING gzip, deflate
HTTP_ACCEPT_LANGUAGE zh-TW
HTTP_HOST localhost
HTTP_REFERER http://localhost/MyHttpWatch/HttpRequestWatcher.aspx
HTTP_USER_AGENT Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; WOW64; Trident/4.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; .NET4.0C; .NET4.0E; MASP)

 
 
 

 
 

Case 3:按下重新整理F5再度Postback的畫面

 
 

 
 

Page_Load回傳的字串:
ALL_HTTP HTTP_CACHE_CONTROL:no-cache HTTP_CONNECTION:Keep-Alive HTTP_CONTENT_LENGTH:5377 HTTP_CONTENT_TYPE:application/x-www-form-urlencoded HTTP_ACCEPT:*/* HTTP_ACCEPT_ENCODING:gzip, deflate HTTP_ACCEPT_LANGUAGE:zh-TW HTTP_HOST:localhost HTTP_REFERER:http://localhost/MyHttpWatch/HttpRequestWatcher.aspx HTTP_USER_AGENT:Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; WOW64; Trident/4.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; .NET4.0C; .NET4.0E; MASP)
ALL_RAW Cache-Control: no-cache Connection: Keep-Alive Content-Length: 5377 Content-Type: application/x-www-form-urlencoded Accept: */* Accept-Encoding: gzip, deflate Accept-Language: zh-TW Host: localhost Referer: http://localhost/MyHttpWatch/HttpRequestWatcher.aspx User-Agent: Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; WOW64; Trident/4.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; .NET4.0C; .NET4.0E; MASP)
APPL_MD_PATH /LM/W3SVC/1/ROOT/MyHttpWatch
APPL_PHYSICAL_PATH D:\MyHttpWatch\
AUTH_TYPE  
AUTH_USER  
AUTH_PASSWORD  
LOGON_USER  
REMOTE_USER  
CERT_COOKIE  
CERT_FLAGS  
CERT_ISSUER  
CERT_KEYSIZE  
CERT_SECRETKEYSIZE  
CERT_SERIALNUMBER  
CERT_SERVER_ISSUER  
CERT_SERVER_SUBJECT  
CERT_SUBJECT  
CONTENT_LENGTH 5377
CONTENT_TYPE application/x-www-form-urlencoded
GATEWAY_INTERFACE CGI/1.1
HTTPS off
HTTPS_KEYSIZE  
HTTPS_SECRETKEYSIZE  
HTTPS_SERVER_ISSUER  
HTTPS_SERVER_SUBJECT  
INSTANCE_ID 1
INSTANCE_META_PATH /LM/W3SVC/1
LOCAL_ADDR ::1
PATH_INFO /MyHttpWatch/HttpRequestWatcher.aspx
PATH_TRANSLATED D:\MyHttpWatch\HttpRequestWatcher.aspx
QUERY_STRING  
REMOTE_ADDR ::1
REMOTE_HOST ::1
REMOTE_PORT 4490
REQUEST_METHOD POST
SCRIPT_NAME /MyHttpWatch/HttpRequestWatcher.aspx
SERVER_NAME localhost
SERVER_PORT 80
SERVER_PORT_SECURE 0
SERVER_PROTOCOL HTTP/1.1
SERVER_SOFTWARE Microsoft-IIS/7.5
URL /MyHttpWatch/HttpRequestWatcher.aspx
HTTP_CACHE_CONTROL no-cache
HTTP_CONNECTION Keep-Alive
HTTP_CONTENT_LENGTH 5377
HTTP_CONTENT_TYPE application/x-www-form-urlencoded
HTTP_ACCEPT */*
HTTP_ACCEPT_ENCODING gzip, deflate
HTTP_ACCEPT_LANGUAGE zh-TW
HTTP_HOST localhost
HTTP_REFERER http://localhost/MyHttpWatch/HttpRequestWatcher.aspx
HTTP_USER_AGENT Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; WOW64; Trident/4.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; .NET4.0C; .NET4.0E; MASP)

 

 
 

Case 4:網頁回到一開始Get Method狀態,按下jQueryAjaxGet的畫面

 
 
 

Page_Load回傳的字串(對照組):

ALL_HTTP HTTP_CONNECTION:Keep-Alive HTTP_ACCEPT:*/* HTTP_ACCEPT_ENCODING:gzip, deflate HTTP_ACCEPT_LANGUAGE:zh-TW HTTP_HOST:localhost HTTP_USER_AGENT:Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; WOW64; Trident/4.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; .NET4.0C; .NET4.0E; MASP)
ALL_RAW Connection: Keep-Alive Accept: */* Accept-Encoding: gzip, deflate Accept-Language: zh-TW Host: localhost User-Agent: Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; WOW64; Trident/4.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; .NET4.0C; .NET4.0E; MASP)
APPL_MD_PATH /LM/W3SVC/1/ROOT/MyHttpWatch
APPL_PHYSICAL_PATH D:\MyHttpWatch\
AUTH_TYPE  
AUTH_USER  
AUTH_PASSWORD  
LOGON_USER  
REMOTE_USER  
CERT_COOKIE  
CERT_FLAGS  
CERT_ISSUER  
CERT_KEYSIZE  
CERT_SECRETKEYSIZE  
CERT_SERIALNUMBER  
CERT_SERVER_ISSUER  
CERT_SERVER_SUBJECT  
CERT_SUBJECT  
CONTENT_LENGTH 0
CONTENT_TYPE  
GATEWAY_INTERFACE CGI/1.1
HTTPS off
HTTPS_KEYSIZE  
HTTPS_SECRETKEYSIZE  
HTTPS_SERVER_ISSUER  
HTTPS_SERVER_SUBJECT  
INSTANCE_ID 1
INSTANCE_META_PATH /LM/W3SVC/1
LOCAL_ADDR ::1
PATH_INFO /MyHttpWatch/HttpRequestWatcher.aspx
PATH_TRANSLATED D:\MyHttpWatch\HttpRequestWatcher.aspx
QUERY_STRING  
REMOTE_ADDR ::1
REMOTE_HOST ::1
REMOTE_PORT 4492
REQUEST_METHOD GET
SCRIPT_NAME /MyHttpWatch/HttpRequestWatcher.aspx
SERVER_NAME localhost
SERVER_PORT 80
SERVER_PORT_SECURE 0
SERVER_PROTOCOL HTTP/1.1
SERVER_SOFTWARE Microsoft-IIS/7.5
URL /MyHttpWatch/HttpRequestWatcher.aspx
HTTP_CONNECTION Keep-Alive
HTTP_ACCEPT */*
HTTP_ACCEPT_ENCODING gzip, deflate
HTTP_ACCEPT_LANGUAGE zh-TW
HTTP_HOST localhost
HTTP_USER_AGENT Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; WOW64; Trident/4.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; .NET4.0C; .NET4.0E; MASP)

 

jQuery區:
Page_Load回傳的字串:
ALL_HTTP HTTP_CONNECTION:Keep-Alive HTTP_CONTENT_TYPE:application/x-www-form-urlencoded HTTP_ACCEPT:*/* HTTP_ACCEPT_ENCODING:gzip, deflate HTTP_ACCEPT_LANGUAGE:zh-tw HTTP_HOST:localhost HTTP_REFERER:http://localhost/MyHttpWatch/HttpRequestWatcher.aspx HTTP_USER_AGENT:Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; WOW64; Trident/4.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; .NET4.0C; .NET4.0E; MASP) HTTP_X_REQUESTED_WITH:XMLHttpRequest
ALL_RAW Connection: Keep-Alive Content-Type: application/x-www-form-urlencoded Accept: */* Accept-Encoding: gzip, deflate Accept-Language: zh-tw Host: localhost Referer: http://localhost/MyHttpWatch/HttpRequestWatcher.aspx User-Agent: Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; WOW64; Trident/4.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; .NET4.0C; .NET4.0E; MASP) x-requested-with: XMLHttpRequest
APPL_MD_PATH /LM/W3SVC/1/ROOT/MyHttpWatch
APPL_PHYSICAL_PATH D:\MyHttpWatch\
AUTH_TYPE  
AUTH_USER  
AUTH_PASSWORD  
LOGON_USER  
REMOTE_USER  
CERT_COOKIE  
CERT_FLAGS  
CERT_ISSUER  
CERT_KEYSIZE  
CERT_SECRETKEYSIZE  
CERT_SERIALNUMBER  
CERT_SERVER_ISSUER  
CERT_SERVER_SUBJECT  
CERT_SUBJECT  
CONTENT_LENGTH 0
CONTENT_TYPE application/x-www-form-urlencoded
GATEWAY_INTERFACE CGI/1.1
HTTPS off
HTTPS_KEYSIZE  
HTTPS_SECRETKEYSIZE  
HTTPS_SERVER_ISSUER  
HTTPS_SERVER_SUBJECT  
INSTANCE_ID 1
INSTANCE_META_PATH /LM/W3SVC/1
LOCAL_ADDR ::1
PATH_INFO /MyHttpWatch/HttpRequestWatcher.aspx
PATH_TRANSLATED D:\MyHttpWatch\HttpRequestWatcher.aspx
QUERY_STRING data=passQueryString
REMOTE_ADDR ::1
REMOTE_HOST ::1
REMOTE_PORT 4492
REQUEST_METHOD GET
SCRIPT_NAME /MyHttpWatch/HttpRequestWatcher.aspx
SERVER_NAME localhost
SERVER_PORT 80
SERVER_PORT_SECURE 0
SERVER_PROTOCOL HTTP/1.1
SERVER_SOFTWARE Microsoft-IIS/7.5
URL /MyHttpWatch/HttpRequestWatcher.aspx
HTTP_CONNECTION Keep-Alive
HTTP_CONTENT_TYPE application/x-www-form-urlencoded
HTTP_ACCEPT */*
HTTP_ACCEPT_ENCODING gzip, deflate
HTTP_ACCEPT_LANGUAGE zh-tw
HTTP_HOST localhost
HTTP_REFERER http://localhost/MyHttpWatch/HttpRequestWatcher.aspx
HTTP_USER_AGENT Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; WOW64; Trident/4.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; .NET4.0C; .NET4.0E; MASP)
HTTP_X_REQUESTED_WITH XMLHttpRequest

 
 

 

 

Case 5:網頁回到一開始Get Method狀態,按下jQueryAjaxPost的畫面

 
 

 
 

Page_Load回傳的字串(對照組): 
ALL_HTTP HTTP_CONNECTION:Keep-Alive HTTP_ACCEPT:*/* HTTP_ACCEPT_ENCODING:gzip, deflate HTTP_ACCEPT_LANGUAGE:zh-TW HTTP_HOST:localhost HTTP_USER_AGENT:Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; WOW64; Trident/4.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; .NET4.0C; .NET4.0E; MASP)
ALL_RAW Connection: Keep-Alive Accept: */* Accept-Encoding: gzip, deflate Accept-Language: zh-TW Host: localhost User-Agent: Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; WOW64; Trident/4.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; .NET4.0C; .NET4.0E; MASP)
APPL_MD_PATH /LM/W3SVC/1/ROOT/MyHttpWatch
APPL_PHYSICAL_PATH D:\MyHttpWatch\
AUTH_TYPE  
AUTH_USER  
AUTH_PASSWORD  
LOGON_USER  
REMOTE_USER  
CERT_COOKIE  
CERT_FLAGS  
CERT_ISSUER  
CERT_KEYSIZE  
CERT_SECRETKEYSIZE  
CERT_SERIALNUMBER  
CERT_SERVER_ISSUER  
CERT_SERVER_SUBJECT  
CERT_SUBJECT  
CONTENT_LENGTH 0
CONTENT_TYPE  
GATEWAY_INTERFACE CGI/1.1
HTTPS off
HTTPS_KEYSIZE  
HTTPS_SECRETKEYSIZE  
HTTPS_SERVER_ISSUER  
HTTPS_SERVER_SUBJECT  
INSTANCE_ID 1
INSTANCE_META_PATH /LM/W3SVC/1
LOCAL_ADDR ::1
PATH_INFO /MyHttpWatch/HttpRequestWatcher.aspx
PATH_TRANSLATED D:\MyHttpWatch\HttpRequestWatcher.aspx
QUERY_STRING  
REMOTE_ADDR ::1
REMOTE_HOST ::1
REMOTE_PORT 4490
REQUEST_METHOD GET
SCRIPT_NAME /MyHttpWatch/HttpRequestWatcher.aspx
SERVER_NAME localhost
SERVER_PORT 80
SERVER_PORT_SECURE 0
SERVER_PROTOCOL HTTP/1.1
SERVER_SOFTWARE Microsoft-IIS/7.5
URL /MyHttpWatch/HttpRequestWatcher.aspx
HTTP_CONNECTION Keep-Alive
HTTP_ACCEPT */*
HTTP_ACCEPT_ENCODING gzip, deflate
HTTP_ACCEPT_LANGUAGE zh-TW
HTTP_HOST localhost
HTTP_USER_AGENT Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; WOW64; Trident/4.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; .NET4.0C; .NET4.0E; MASP)



jQuery區:
Page_Load回傳的字串:
ALL_HTTP HTTP_CACHE_CONTROL:no-cache HTTP_CONNECTION:Keep-Alive HTTP_CONTENT_LENGTH:17 HTTP_CONTENT_TYPE:application/x-www-form-urlencoded HTTP_ACCEPT:*/* HTTP_ACCEPT_ENCODING:gzip, deflate HTTP_ACCEPT_LANGUAGE:zh-tw HTTP_HOST:localhost HTTP_REFERER:http://localhost/MyHttpWatch/HttpRequestWatcher.aspx HTTP_USER_AGENT:Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; WOW64; Trident/4.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; .NET4.0C; .NET4.0E; MASP) HTTP_X_REQUESTED_WITH:XMLHttpRequest
ALL_RAW Cache-Control: no-cache Connection: Keep-Alive Content-Length: 17 Content-Type: application/x-www-form-urlencoded Accept: */* Accept-Encoding: gzip, deflate Accept-Language: zh-tw Host: localhost Referer: http://localhost/MyHttpWatch/HttpRequestWatcher.aspx User-Agent: Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; WOW64; Trident/4.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; .NET4.0C; .NET4.0E; MASP) x-requested-with: XMLHttpRequest
APPL_MD_PATH /LM/W3SVC/1/ROOT/MyHttpWatch
APPL_PHYSICAL_PATH D:\MyHttpWatch\
AUTH_TYPE  
AUTH_USER  
AUTH_PASSWORD  
LOGON_USER  
REMOTE_USER  
CERT_COOKIE  
CERT_FLAGS  
CERT_ISSUER  
CERT_KEYSIZE  
CERT_SECRETKEYSIZE  
CERT_SERIALNUMBER  
CERT_SERVER_ISSUER  
CERT_SERVER_SUBJECT  
CERT_SUBJECT  
CONTENT_LENGTH 17
CONTENT_TYPE application/x-www-form-urlencoded
GATEWAY_INTERFACE CGI/1.1
HTTPS off
HTTPS_KEYSIZE  
HTTPS_SECRETKEYSIZE  
HTTPS_SERVER_ISSUER  
HTTPS_SERVER_SUBJECT  
INSTANCE_ID 1
INSTANCE_META_PATH /LM/W3SVC/1
LOCAL_ADDR ::1
PATH_INFO /MyHttpWatch/HttpRequestWatcher.aspx
PATH_TRANSLATED D:\MyHttpWatch\HttpRequestWatcher.aspx
QUERY_STRING  
REMOTE_ADDR ::1
REMOTE_HOST ::1
REMOTE_PORT 4492
REQUEST_METHOD POST
SCRIPT_NAME /MyHttpWatch/HttpRequestWatcher.aspx
SERVER_NAME localhost
SERVER_PORT 80
SERVER_PORT_SECURE 0
SERVER_PROTOCOL HTTP/1.1
SERVER_SOFTWARE Microsoft-IIS/7.5
URL /MyHttpWatch/HttpRequestWatcher.aspx
HTTP_CACHE_CONTROL no-cache
HTTP_CONNECTION Keep-Alive
HTTP_CONTENT_LENGTH 17
HTTP_CONTENT_TYPE application/x-www-form-urlencoded
HTTP_ACCEPT */*
HTTP_ACCEPT_ENCODING gzip, deflate
HTTP_ACCEPT_LANGUAGE zh-tw
HTTP_HOST localhost
HTTP_REFERER http://localhost/MyHttpWatch/HttpRequestWatcher.aspx
HTTP_USER_AGENT Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; WOW64; Trident/4.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; .NET4.0C; .NET4.0E; MASP)
HTTP_X_REQUESTED_WITH XMLHttpRequest

   


Case 6:網頁回到一開始Get Method狀態,按下UpdatePanel按鈕局部更新的畫面

 
 

Page_Load回傳的字串(對照組): 
ALL_HTTP HTTP_CONNECTION:Keep-Alive HTTP_ACCEPT:*/* HTTP_ACCEPT_ENCODING:gzip, deflate HTTP_ACCEPT_LANGUAGE:zh-TW HTTP_HOST:localhost HTTP_USER_AGENT:Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; WOW64; Trident/4.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; .NET4.0C; .NET4.0E; MASP)
ALL_RAW Connection: Keep-Alive Accept: */* Accept-Encoding: gzip, deflate Accept-Language: zh-TW Host: localhost User-Agent: Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; WOW64; Trident/4.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; .NET4.0C; .NET4.0E; MASP)
APPL_MD_PATH /LM/W3SVC/1/ROOT/MyHttpWatch
APPL_PHYSICAL_PATH D:\MyHttpWatch\
AUTH_TYPE  
AUTH_USER  
AUTH_PASSWORD  
LOGON_USER  
REMOTE_USER  
CERT_COOKIE  
CERT_FLAGS  
CERT_ISSUER  
CERT_KEYSIZE  
CERT_SECRETKEYSIZE  
CERT_SERIALNUMBER  
CERT_SERVER_ISSUER  
CERT_SERVER_SUBJECT  
CERT_SUBJECT  
CONTENT_LENGTH 0
CONTENT_TYPE  
GATEWAY_INTERFACE CGI/1.1
HTTPS off
HTTPS_KEYSIZE  
HTTPS_SECRETKEYSIZE  
HTTPS_SERVER_ISSUER  
HTTPS_SERVER_SUBJECT  
INSTANCE_ID 1
INSTANCE_META_PATH /LM/W3SVC/1
LOCAL_ADDR ::1
PATH_INFO /MyHttpWatch/HttpRequestWatcher.aspx
PATH_TRANSLATED D:\MyHttpWatch\HttpRequestWatcher.aspx
QUERY_STRING  
REMOTE_ADDR ::1
REMOTE_HOST ::1
REMOTE_PORT 4492
REQUEST_METHOD GET
SCRIPT_NAME /MyHttpWatch/HttpRequestWatcher.aspx
SERVER_NAME localhost
SERVER_PORT 80
SERVER_PORT_SECURE 0
SERVER_PROTOCOL HTTP/1.1
SERVER_SOFTWARE Microsoft-IIS/7.5
URL /MyHttpWatch/HttpRequestWatcher.aspx
HTTP_CONNECTION Keep-Alive
HTTP_ACCEPT */*
HTTP_ACCEPT_ENCODING gzip, deflate
HTTP_ACCEPT_LANGUAGE zh-TW
HTTP_HOST localhost
HTTP_USER_AGENT Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; WOW64; Trident/4.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; .NET4.0C; .NET4.0E; MASP)

 

ASP.net UpdatePanel區:
 
ALL_HTTP HTTP_CACHE_CONTROL:no-cache HTTP_CONNECTION:Keep-Alive HTTP_CONTENT_LENGTH:4775 HTTP_CONTENT_TYPE:application/x-www-form-urlencoded; charset=utf-8 HTTP_ACCEPT:*/* HTTP_ACCEPT_ENCODING:gzip, deflate HTTP_ACCEPT_LANGUAGE:zh-tw HTTP_HOST:localhost HTTP_REFERER:http://localhost/MyHttpWatch/HttpRequestWatcher.aspx HTTP_USER_AGENT:Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; WOW64; Trident/4.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; .NET4.0C; .NET4.0E; MASP) HTTP_X_REQUESTED_WITH:XMLHttpRequest HTTP_X_MICROSOFTAJAX:Delta=true
ALL_RAW Cache-Control: no-cache Connection: Keep-Alive Content-Length: 4775 Content-Type: application/x-www-form-urlencoded; charset=utf-8 Accept: */* Accept-Encoding: gzip, deflate Accept-Language: zh-tw Host: localhost Referer: http://localhost/MyHttpWatch/HttpRequestWatcher.aspx User-Agent: Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; WOW64; Trident/4.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; .NET4.0C; .NET4.0E; MASP) x-requested-with: XMLHttpRequest x-microsoftajax: Delta=true
APPL_MD_PATH /LM/W3SVC/1/ROOT/MyHttpWatch
APPL_PHYSICAL_PATH D:\MyHttpWatch\
AUTH_TYPE  
AUTH_USER  
AUTH_PASSWORD  
LOGON_USER  
REMOTE_USER  
CERT_COOKIE  
CERT_FLAGS  
CERT_ISSUER  
CERT_KEYSIZE  
CERT_SECRETKEYSIZE  
CERT_SERIALNUMBER  
CERT_SERVER_ISSUER  
CERT_SERVER_SUBJECT  
CERT_SUBJECT  
CONTENT_LENGTH 4775
CONTENT_TYPE application/x-www-form-urlencoded; charset=utf-8
GATEWAY_INTERFACE CGI/1.1
HTTPS off
HTTPS_KEYSIZE  
HTTPS_SECRETKEYSIZE  
HTTPS_SERVER_ISSUER  
HTTPS_SERVER_SUBJECT  
INSTANCE_ID 1
INSTANCE_META_PATH /LM/W3SVC/1
LOCAL_ADDR ::1
PATH_INFO /MyHttpWatch/HttpRequestWatcher.aspx
PATH_TRANSLATED D:\MyHttpWatch\HttpRequestWatcher.aspx
QUERY_STRING  
REMOTE_ADDR ::1
REMOTE_HOST ::1
REMOTE_PORT 4492
REQUEST_METHOD POST
SCRIPT_NAME /MyHttpWatch/HttpRequestWatcher.aspx
SERVER_NAME localhost
SERVER_PORT 80
SERVER_PORT_SECURE 0
SERVER_PROTOCOL HTTP/1.1
SERVER_SOFTWARE Microsoft-IIS/7.5
URL /MyHttpWatch/HttpRequestWatcher.aspx
HTTP_CACHE_CONTROL no-cache
HTTP_CONNECTION Keep-Alive
HTTP_CONTENT_LENGTH 4775
HTTP_CONTENT_TYPE application/x-www-form-urlencoded; charset=utf-8
HTTP_ACCEPT */*
HTTP_ACCEPT_ENCODING gzip, deflate
HTTP_ACCEPT_LANGUAGE zh-tw
HTTP_HOST localhost
HTTP_REFERER http://localhost/MyHttpWatch/HttpRequestWatcher.aspx
HTTP_USER_AGENT Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; WOW64; Trident/4.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; .NET4.0C; .NET4.0E; MASP)
HTTP_X_REQUESTED_WITH XMLHttpRequest
HTTP_X_MICROSOFTAJAX Delta=true

 
 

這邊畫面上直接貼HTML按鈕的話,點部落會讀取很久,所以以上只保留HTML Table資訊。

差異點比較:

1. 判斷是Get Method或Post Method可以傳REQUEST_METHOD鍵值判斷
   (Update Panel的按鈕局部Postback的REQUEST_METHOD值為POST)

2. 一般Postback和F5重新整理的Postback差異可用REQUEST_METHOD來判斷都是POST
   但HTTP_ACCEPT的話,F5重新整理則為「*/*」

※2012.3.31 追記:IE9沒辦法這樣判斷是不是重新整理的樣子?

替代方案:請問重新整理頁面要如何不觸發ListView的ItemCommand事件呢?

3. 是不是jQueryAjax(或UpdatePanel)發出的Request則看有沒有HTTP_X_REQUESTED_WITH鍵值,有的話再判斷字串是不是「XMLHttpRequest」

 4. 判斷Request過來的是不是行動裝置,可從HTTP_USER_AGENT的Value觀察

 

 

其他想到的再不定期補充~

程式碼懶人包下載