HttpRequestBase類別 屬性演示

 

先準備範例,程式碼很長,甚入

前端 index.cshtml

@{
    ViewBag.Title = "Index";

    var result = (Dictionary<string, string>)Model;
    var browser = (Dictionary<string, string>)ViewBag.browser;
    var headers = (Dictionary<string, string>)ViewBag.headers;
    var _params = (Dictionary<string, string>)ViewBag._params;
    var serverVariables = (Dictionary<string, string>)ViewBag.serverVariables;
}
<h2>@ViewBag.Title.</h2>

<table style="border: 1px solid;">

    <tr>
        <th>Request attributes</th>
        <th></th>
    </tr>

    @{
        foreach (var item in result)
        {
            <tr>
                <td>@item.Key</td>
                <td>@item.Value</td>
            </tr>
        }
    }
</table>
<p></p>
<table style="border: 1px solid;">

    <tr>
        <th>Request.Browser attributes</th>
        <th></th>
    </tr>

    @{
        foreach (var item in browser)
        {
            <tr>
                <td>@item.Key</td>
                <td>@item.Value</td>
            </tr>
        }
    }
</table>
<p></p>
<table style="border: 1px solid;">

    <tr>
        <th>Request.Headers attributes</th>
        <th></th>
    </tr>

    @{
        foreach (var item in headers)
        {
            <tr>
                <td>@item.Key</td>
                <td>@item.Value</td>
            </tr>
        }
    }
</table>
<p></p>
<table style="border: 1px solid;">

    <tr>
        <th>Request.Params attributes</th>
        <th></th>
    </tr>

    @{
        foreach (var item in _params)
        {
            <tr>
                <td>@item.Key</td>
                <td>@item.Value</td>
            </tr>
        }
    }
</table>
<p></p>
<table style="border: 1px solid;">

    <tr>
        <th>Request.serverVariables attributes</th>
        <th></th>
    </tr>

    @{
        foreach (var item in serverVariables)
        {
            <tr>
                <td>@item.Key</td>
                <td>@item.Value</td>
            </tr>
        }
    }
</table>

@section styles{
    <style>
        table, th, td {
            border: 1px solid;
            border-collapse: separate;
        }
    </style>
}

 

後端 HomeController.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace WebApplication1.Controllers
{
    public class HomeController : Controller
    {
        public ActionResult Index()
        {
            Dictionary<string, string> result = new Dictionary<string, string>();

            result.Add(nameof(Request.AcceptTypes), string.Join(" ", Request.AcceptTypes));
            result.Add(nameof(Request.AnonymousID), Request.AnonymousID);
            result.Add(nameof(Request.ApplicationPath), Request.ApplicationPath);
            result.Add(nameof(Request.AppRelativeCurrentExecutionFilePath), Request.AppRelativeCurrentExecutionFilePath);

            HttpBrowserCapabilitiesBase Browser = Request.Browser;
            Dictionary<string, string> browser = new Dictionary<string, string>();
            browser.Add(nameof(Request.Browser.ActiveXControls), Request.Browser.ActiveXControls.ToString());
            browser.Add(nameof(Request.Browser.Adapters) + " Count", Request.Browser.Adapters.Count.ToString());
            browser.Add(nameof(Request.Browser.AOL), Request.Browser.AOL.ToString());
            browser.Add(nameof(Request.Browser.BackgroundSounds), Request.Browser.BackgroundSounds.ToString());
            browser.Add(nameof(Request.Browser.Beta), Request.Browser.Beta.ToString());
            browser.Add(nameof(Request.Browser.Browser), Request.Browser.Browser);
            browser.Add(nameof(Request.Browser.Browsers) + " Count", Request.Browser.Browsers.Count.ToString());
            browser.Add(nameof(Request.Browser.CanCombineFormsInDeck), Request.Browser.CanCombineFormsInDeck.ToString());
            browser.Add(nameof(Request.Browser.CanInitiateVoiceCall), Request.Browser.CanInitiateVoiceCall.ToString());
            browser.Add(nameof(Request.Browser.CanRenderAfterInputOrSelectElement), Request.Browser.CanRenderAfterInputOrSelectElement.ToString());
            browser.Add(nameof(Request.Browser.CanRenderEmptySelects), Request.Browser.CanRenderEmptySelects.ToString());
            browser.Add(nameof(Request.Browser.CanRenderInputAndSelectElementsTogether), Request.Browser.CanRenderInputAndSelectElementsTogether.ToString());
            browser.Add(nameof(Request.Browser.CanRenderMixedSelects), Request.Browser.CanRenderMixedSelects.ToString());
            browser.Add(nameof(Request.Browser.CanRenderOneventAndPrevElementsTogether), Request.Browser.CanRenderOneventAndPrevElementsTogether.ToString());
            browser.Add(nameof(Request.Browser.CanRenderPostBackCards), Request.Browser.CanRenderPostBackCards.ToString());
            browser.Add(nameof(Request.Browser.CanRenderSetvarZeroWithMultiSelectionList), Request.Browser.CanRenderSetvarZeroWithMultiSelectionList.ToString());
            browser.Add(nameof(Request.Browser.CanSendMail), Request.Browser.CanSendMail.ToString());
            browser.Add(nameof(Request.Browser.Capabilities) + " Count", Request.Browser.Capabilities.Count.ToString());
            browser.Add(nameof(Request.Browser.CDF), Request.Browser.CDF.ToString());
            browser.Add(nameof(Request.Browser.ClrVersion) + " Build", Request.Browser.ClrVersion.Build.ToString());
            browser.Add(nameof(Request.Browser.Cookies), Request.Browser.Cookies.ToString());
            browser.Add(nameof(Request.Browser.Crawler), Request.Browser.Crawler.ToString());
            browser.Add(nameof(Request.Browser.DefaultSubmitButtonLimit), Request.Browser.DefaultSubmitButtonLimit.ToString());
            browser.Add(nameof(Request.Browser.EcmaScriptVersion) + " Major", Request.Browser.EcmaScriptVersion.Major.ToString());
            browser.Add(nameof(Request.Browser.Frames), Request.Browser.Frames.ToString());
            browser.Add(nameof(Request.Browser.GatewayMajorVersion), Request.Browser.GatewayMajorVersion.ToString());
            browser.Add(nameof(Request.Browser.GatewayMinorVersion), Request.Browser.GatewayMinorVersion.ToString());
            browser.Add(nameof(Request.Browser.GatewayVersion), Request.Browser.GatewayVersion);
            browser.Add(nameof(Request.Browser.HasBackButton), Request.Browser.HasBackButton.ToString());
            browser.Add(nameof(Request.Browser.HidesRightAlignedMultiselectScrollbars), Request.Browser.HidesRightAlignedMultiselectScrollbars.ToString());
            browser.Add(nameof(Request.Browser.HtmlTextWriter), Request.Browser.HtmlTextWriter);
            browser.Add(nameof(Request.Browser.Id), Request.Browser.Id);
            browser.Add(nameof(Request.Browser.InputType), Request.Browser.InputType);
            browser.Add(nameof(Request.Browser.IsColor), Request.Browser.IsColor.ToString());
            browser.Add(nameof(Request.Browser.IsMobileDevice), Request.Browser.IsMobileDevice.ToString());
            browser.Add(nameof(Request.Browser.JavaApplets), Request.Browser.JavaApplets.ToString());
            browser.Add(nameof(Request.Browser.JScriptVersion) + " Major", Request.Browser.JScriptVersion.Major.ToString());
            browser.Add(nameof(Request.Browser.MajorVersion), Request.Browser.MajorVersion.ToString());
            browser.Add(nameof(Request.Browser.MaximumHrefLength), Request.Browser.MaximumHrefLength.ToString());
            browser.Add(nameof(Request.Browser.MaximumRenderedPageSize), Request.Browser.MaximumRenderedPageSize.ToString());
            browser.Add(nameof(Request.Browser.MaximumSoftkeyLabelLength), Request.Browser.MaximumSoftkeyLabelLength.ToString());
            browser.Add(nameof(Request.Browser.MinorVersion), Request.Browser.MinorVersion.ToString());
            browser.Add(nameof(Request.Browser.MinorVersionString), Request.Browser.MinorVersionString);
            browser.Add(nameof(Request.Browser.MobileDeviceManufacturer), Request.Browser.MobileDeviceManufacturer);
            browser.Add(nameof(Request.Browser.MobileDeviceModel), Request.Browser.MobileDeviceModel);
            browser.Add(nameof(Request.Browser.MSDomVersion) + " Major", Request.Browser.MSDomVersion.Major.ToString());
            browser.Add(nameof(Request.Browser.NumberOfSoftkeys), Request.Browser.NumberOfSoftkeys.ToString());
            browser.Add(nameof(Request.Browser.Platform), Request.Browser.Platform);
            browser.Add(nameof(Request.Browser.PreferredImageMime), Request.Browser.PreferredImageMime);
            browser.Add(nameof(Request.Browser.PreferredRenderingMime), Request.Browser.PreferredRenderingMime);
            browser.Add(nameof(Request.Browser.PreferredRenderingType), Request.Browser.PreferredRenderingType);
            browser.Add(nameof(Request.Browser.PreferredRequestEncoding), Request.Browser.PreferredRequestEncoding);
            browser.Add(nameof(Request.Browser.PreferredResponseEncoding), Request.Browser.PreferredResponseEncoding);
            browser.Add(nameof(Request.Browser.RendersBreakBeforeWmlSelectAndInput), Request.Browser.RendersBreakBeforeWmlSelectAndInput.ToString());
            browser.Add(nameof(Request.Browser.RendersBreaksAfterHtmlLists), Request.Browser.RendersBreaksAfterHtmlLists.ToString());
            browser.Add(nameof(Request.Browser.RendersBreaksAfterWmlAnchor), Request.Browser.RendersBreaksAfterWmlAnchor.ToString());
            browser.Add(nameof(Request.Browser.RendersBreaksAfterWmlInput), Request.Browser.RendersBreaksAfterWmlInput.ToString());
            browser.Add(nameof(Request.Browser.RendersWmlDoAcceptsInline), Request.Browser.RendersWmlDoAcceptsInline.ToString());
            browser.Add(nameof(Request.Browser.RendersWmlSelectsAsMenuCards), Request.Browser.RendersWmlSelectsAsMenuCards.ToString());
            browser.Add(nameof(Request.Browser.RequiredMetaTagNameValue), Request.Browser.RequiredMetaTagNameValue);
            browser.Add(nameof(Request.Browser.RequiresAttributeColonSubstitution), Request.Browser.RequiresAttributeColonSubstitution.ToString());
            browser.Add(nameof(Request.Browser.RequiresContentTypeMetaTag), Request.Browser.RequiresContentTypeMetaTag.ToString());
            browser.Add(nameof(Request.Browser.RequiresControlStateInSession), Request.Browser.RequiresControlStateInSession.ToString());
            browser.Add(nameof(Request.Browser.RequiresDBCSCharacter), Request.Browser.RequiresDBCSCharacter.ToString());
            browser.Add(nameof(Request.Browser.RequiresHtmlAdaptiveErrorReporting), Request.Browser.RequiresHtmlAdaptiveErrorReporting.ToString());
            browser.Add(nameof(Request.Browser.RequiresLeadingPageBreak), Request.Browser.RequiresLeadingPageBreak.ToString());
            browser.Add(nameof(Request.Browser.RequiresNoBreakInFormatting), Request.Browser.RequiresNoBreakInFormatting.ToString());
            browser.Add(nameof(Request.Browser.RequiresOutputOptimization), Request.Browser.RequiresOutputOptimization.ToString());
            browser.Add(nameof(Request.Browser.RequiresPhoneNumbersAsPlainText), Request.Browser.RequiresPhoneNumbersAsPlainText.ToString());
            browser.Add(nameof(Request.Browser.RequiresSpecialViewStateEncoding), Request.Browser.RequiresSpecialViewStateEncoding.ToString());
            browser.Add(nameof(Request.Browser.RequiresUniqueFilePathSuffix), Request.Browser.RequiresUniqueFilePathSuffix.ToString());
            browser.Add(nameof(Request.Browser.RequiresUniqueHtmlCheckboxNames), Request.Browser.RequiresUniqueHtmlCheckboxNames.ToString());
            browser.Add(nameof(Request.Browser.RequiresUniqueHtmlInputNames), Request.Browser.RequiresUniqueHtmlInputNames.ToString());
            browser.Add(nameof(Request.Browser.RequiresUrlEncodedPostfieldValues), Request.Browser.RequiresUrlEncodedPostfieldValues.ToString());
            browser.Add(nameof(Request.Browser.ScreenBitDepth), Request.Browser.ScreenBitDepth.ToString());
            browser.Add(nameof(Request.Browser.ScreenCharactersHeight), Request.Browser.ScreenCharactersHeight.ToString());
            browser.Add(nameof(Request.Browser.ScreenCharactersWidth), Request.Browser.ScreenCharactersWidth.ToString());
            browser.Add(nameof(Request.Browser.ScreenPixelsHeight), Request.Browser.ScreenPixelsHeight.ToString());
            browser.Add(nameof(Request.Browser.ScreenPixelsWidth), Request.Browser.ScreenPixelsWidth.ToString());
            browser.Add(nameof(Request.Browser.SupportsAccesskeyAttribute), Request.Browser.SupportsAccesskeyAttribute.ToString());
            browser.Add(nameof(Request.Browser.SupportsBodyColor), Request.Browser.SupportsBodyColor.ToString());
            browser.Add(nameof(Request.Browser.SupportsBold), Request.Browser.SupportsBold.ToString());
            browser.Add(nameof(Request.Browser.SupportsCacheControlMetaTag), Request.Browser.SupportsCacheControlMetaTag.ToString());
            browser.Add(nameof(Request.Browser.SupportsCallback), Request.Browser.SupportsCallback.ToString());
            browser.Add(nameof(Request.Browser.SupportsCss), Request.Browser.SupportsCss.ToString());
            browser.Add(nameof(Request.Browser.SupportsDivAlign), Request.Browser.SupportsDivAlign.ToString());
            browser.Add(nameof(Request.Browser.SupportsDivNoWrap), Request.Browser.SupportsDivNoWrap.ToString());
            browser.Add(nameof(Request.Browser.SupportsEmptyStringInCookieValue), Request.Browser.SupportsEmptyStringInCookieValue.ToString());
            browser.Add(nameof(Request.Browser.SupportsFontColor), Request.Browser.SupportsFontColor.ToString());
            browser.Add(nameof(Request.Browser.SupportsFontName), Request.Browser.SupportsFontName.ToString());
            browser.Add(nameof(Request.Browser.SupportsFontSize), Request.Browser.SupportsFontSize.ToString());
            browser.Add(nameof(Request.Browser.SupportsImageSubmit), Request.Browser.SupportsImageSubmit.ToString());
            browser.Add(nameof(Request.Browser.SupportsIModeSymbols), Request.Browser.SupportsIModeSymbols.ToString());
            browser.Add(nameof(Request.Browser.SupportsInputIStyle), Request.Browser.SupportsInputIStyle.ToString());
            browser.Add(nameof(Request.Browser.SupportsInputMode), Request.Browser.SupportsInputMode.ToString());
            browser.Add(nameof(Request.Browser.SupportsItalic), Request.Browser.SupportsItalic.ToString());
            browser.Add(nameof(Request.Browser.SupportsJPhoneMultiMediaAttributes), Request.Browser.SupportsJPhoneMultiMediaAttributes.ToString());
            browser.Add(nameof(Request.Browser.SupportsJPhoneSymbols), Request.Browser.SupportsJPhoneSymbols.ToString());
            browser.Add(nameof(Request.Browser.SupportsQueryStringInFormAction), Request.Browser.SupportsQueryStringInFormAction.ToString());
            browser.Add(nameof(Request.Browser.SupportsRedirectWithCookie), Request.Browser.SupportsRedirectWithCookie.ToString());
            browser.Add(nameof(Request.Browser.SupportsSelectMultiple), Request.Browser.SupportsSelectMultiple.ToString());
            browser.Add(nameof(Request.Browser.SupportsUncheck), Request.Browser.SupportsUncheck.ToString());
            browser.Add(nameof(Request.Browser.SupportsXmlHttp), Request.Browser.SupportsXmlHttp.ToString());
            browser.Add(nameof(Request.Browser.Tables), Request.Browser.Tables.ToString());
            browser.Add(nameof(Request.Browser.TagWriter), Request.Browser.TagWriter.FullName);
            browser.Add(nameof(Request.Browser.Type), Request.Browser.Type);
            browser.Add(nameof(Request.Browser.UseOptimizedCacheKey), Request.Browser.UseOptimizedCacheKey.ToString());
            browser.Add(nameof(Request.Browser.VBScript), Request.Browser.VBScript.ToString());
            browser.Add(nameof(Request.Browser.Version), Request.Browser.Version);
            browser.Add(nameof(Request.Browser.W3CDomVersion) + " Major", Request.Browser.W3CDomVersion.Major.ToString());
            browser.Add(nameof(Request.Browser.Win16), Request.Browser.Win16.ToString());
            browser.Add(nameof(Request.Browser.Win32), Request.Browser.Win32.ToString());

            HttpClientCertificate ClientCertificate = Request.ClientCertificate;
            result.Add(nameof(Request.ClientCertificate) + " Count", Request.ClientCertificate.Count.ToString());

            result.Add(nameof(Request.ContentEncoding), Request.ContentEncoding.ToString());
            result.Add(nameof(Request.ContentLength), Request.ContentLength.ToString());
            result.Add(nameof(Request.ContentType), Request.ContentType);

            HttpCookieCollection Cookies = Request.Cookies;
            result.Add(nameof(Request.Cookies) + " Count", Request.Cookies.Count.ToString());

            result.Add(nameof(Request.CurrentExecutionFilePath), Request.CurrentExecutionFilePath);
            result.Add(nameof(Request.CurrentExecutionFilePathExtension), Request.CurrentExecutionFilePathExtension);
            result.Add(nameof(Request.FilePath), Request.FilePath);

            HttpFileCollectionBase Files = Request.Files;
            result.Add(nameof(Request.Files) + " Count", Request.Files.Count.ToString());

            System.IO.Stream Filter = Request.Filter;
            result.Add(nameof(Request.Filter) + " Length", Request.Filter.Length.ToString());

            System.Collections.Specialized.NameValueCollection Form = Request.Form;
            result.Add(nameof(Request.Form) + " Count", Request.Form.Count.ToString());

            Dictionary<string, string> headers = new Dictionary<string, string>();
            System.Collections.Specialized.NameValueCollection Headers = Request.Headers;
            foreach (var item in Request.Headers.AllKeys)
            {
                headers.Add(nameof(Request.Headers) + "." + item, Request.Headers[item]);
            }

            System.Security.Authentication.ExtendedProtection.ChannelBinding HttpChannelBinding = Request.HttpChannelBinding;
            result.Add(nameof(Request.HttpChannelBinding) + " Size", Request.HttpChannelBinding.Size.ToString());

            result.Add(nameof(Request.HttpMethod), Request.HttpMethod);

            System.IO.Stream InputStream = Request.InputStream;
            result.Add(nameof(Request.InputStream), Request.InputStream.Length.ToString());

            result.Add(nameof(Request.IsAuthenticated), Request.IsAuthenticated.ToString());
            result.Add(nameof(Request.IsLocal), Request.IsLocal.ToString());
            result.Add(nameof(Request.IsSecureConnection), Request.IsSecureConnection.ToString());

            //Request.Item[String]

            System.Security.Principal.WindowsIdentity LogonUserIdentity = Request.LogonUserIdentity;
            result.Add(nameof(Request.LogonUserIdentity) + " Name", Request.LogonUserIdentity.Name.ToString());

            Dictionary<string, string> _params = new Dictionary<string, string>();
            System.Collections.Specialized.NameValueCollection Params = Request.Params;
            foreach (var item in Request.Params.AllKeys)
            {
                _params.Add(nameof(Request.Params) + "." + item, Request.Params[item]);
            }

            result.Add(nameof(Request.Path), Request.Path);
            result.Add(nameof(Request.PathInfo), Request.PathInfo);
            result.Add(nameof(Request.PhysicalApplicationPath), Request.PhysicalApplicationPath);
            result.Add(nameof(Request.PhysicalPath), Request.PhysicalPath);

            System.Collections.Specialized.NameValueCollection QueryString = Request.QueryString;
            result.Add(nameof(Request.QueryString) + " Count", Request.QueryString.Count.ToString());

            result.Add(nameof(Request.RawUrl), Request.RawUrl);

            System.Web.ReadEntityBodyMode ReadEntityBodyMode = Request.ReadEntityBodyMode;
            result.Add(nameof(Request.ReadEntityBodyMode), Request.ReadEntityBodyMode.ToString());

            System.Web.Routing.RequestContext RequestContext = Request.RequestContext;

            result.Add(nameof(Request.RequestType), Request.RequestType);

            Dictionary<string, string> serverVariables = new Dictionary<string, string>();
            System.Collections.Specialized.NameValueCollection ServerVariables = Request.ServerVariables;
            foreach (var item in Request.ServerVariables.AllKeys)
            {
                serverVariables.Add(nameof(Request.ServerVariables) + "." + item, Request.ServerVariables[item]);
            }

            System.Threading.CancellationToken TimedOutToken = Request.TimedOutToken;
            result.Add(nameof(Request.TimedOutToken) + ".IsCancellationRequested", Request.TimedOutToken.IsCancellationRequested.ToString());

            result.Add(nameof(Request.TotalBytes), Request.TotalBytes.ToString());

            UnvalidatedRequestValuesBase Unvalidated = Request.Unvalidated;
            Uri Url = Request.Url;
            Uri UrlReferrer = Request.UrlReferrer;

            result.Add(nameof(Request.UserAgent), Request.UserAgent);
            result.Add(nameof(Request.UserHostAddress), Request.UserHostAddress);
            result.Add(nameof(Request.UserHostName), Request.UserHostName);
            result.Add(nameof(Request.UserLanguages), string.Join(" ", Request.UserLanguages));

            ViewBag.browser = browser;
            ViewBag.headers = headers;
            ViewBag._params = _params;
            ViewBag.serverVariables = serverVariables;

            return View(result);
        }
    }
}

HttpRequestBase 類別 (就是Controller底下的Request屬性) 可以用來

1、取使用者IP (UserHostAddress)

2、判斷使用者的作業系統、瀏灠器版本為何 (UserAgent)

3、判斷使用者是否使用手持裝置 (IsMobileDevice)

4、是否開啟或支援java、JavaScript (Request.Browser.JavaApplets)

5、取得cookies、QueryString、header等資訊

 

HttpRequestBase 類別的屬性很多,但並不是所有資訊都正確,僅供參考。

 

參考資料:

HttpRequestBase 類別

Request.ServerVariables Collection

列出 Request.ServerVariables

如何正確的取得使用者 IP?

HttpRequest 類別 (System.Web)

HttpResponse 類別 (System.Web)