共用方式為


HTTP 通訊協定設定 < HTTPProtocol>

概觀

元素 <httpProtocol> 會設定 HTTP 保持連線,以及 Internet Information Services (IIS) 7 傳送至 Web 用戶端的自訂和重新導向回應標頭。

瀏覽器通常會提出多個要求,以下載整個網頁。 為了增強伺服器效能,大部分的網頁瀏覽器會要求伺服器在這些多個要求之間保持連線開啟,這是稱為 HTTP keep-alives 的功能。 如果沒有 HTTP 保持運作,瀏覽器會對包含多個元素的頁面提出許多要求,例如圖形,可能需要個別連接每個元素。 這些額外的要求和連線需要額外的伺服器活動和資源,以降低伺服器效率。 其他連線也會讓瀏覽器變慢且回應較不快,特別是在緩慢連線時。

相容性

版本 備註
IIS 10.0 <httpProtocol> IIS 10.0 中未修改專案。
IIS 8.5 <httpProtocol> 在 IIS 8.5 中修改專案。
IIS 8.0 在 IIS 8.0 中未修改專案 <httpProtocol>
IIS 7.5 <httpProtocol> 在 IIS 7.5 中修改專案。
IIS 7.0 專案 <httpProtocol> 是在 IIS 7.0 中引進的。
IIS 6.0 專案的 allowKeepAlive 屬性 <httpProtocol> 會取代 IIS 6.0 AllowKeepAlive 中繼基底屬性。

安裝程式

專案 <httpProtocol> 包含在 IIS 7 的預設安裝中。

作法

如何啟用網站或應用程式的 HTTP 保持運作

  1. (IIS) 管理員開啟 Internet Information Services

    • 如果您使用 Windows Server 2012 或 Windows Server 2012 R2:

      • 在工作列上,依序按一下 [伺服器管理員]、[工具],然後按一下 [Internet Information Services] ([IIS) 管理員]。
    • 如果您使用 Windows 8 或 Windows 8.1:

      • 按住Windows鍵,按字母X,然後按一下[主控台]。
      • 按一下 [ 系統管理工具],然後按兩下 [Internet Information Services] ([IIS) 管理員]。
    • 如果您使用 Windows Server 2008 或 Windows Server 2008 R2:

      • 在工作列上,按一下 [ 開始],指向 [ 系統管理工具],然後按一下 [ Internet Information Services (IIS) 管理員]。
    • 如果您使用 Windows Vista 或 Windows 7:

      • 在工作列上,按一下 [開始],然後按一下[主控台]。
      • 按兩下 [ 系統管理工具],然後按兩下 [Internet Information Services] ([IIS) 管理員]。
  2. 在 [ 連線 ] 窗格中,移至您要啟用 HTTP keep-alives 的月臺、應用程式或目錄。

  3. 在 [ 首頁] 窗格中,按兩下 [HTTP 回應標頭]。
    預設網站首頁的螢幕擷取畫面。H T T P 回應標頭圖示會反白顯示。

  4. 在 [HTTP 回應標頭]窗格中,按一下 [動作] 窗格中的 [設定通用標頭...]。
    H T T P 回應標頭頁面的螢幕擷取畫面。

  5. 在 [ 設定一般 HTTP 回應標頭 ] 對話方塊中,核取方塊以啟用 HTTP keep-alives,然後按一下 [ 確定]。
    [設定一般 H T T P 回應標頭] 對話方塊的螢幕擷取畫面。

組態

屬性

屬性 描述
allowKeepAlive 選擇性的 Boolean 屬性。

指定是否允許在 true (true) (false) 。

預設值是 true

子元素

元素 描述
customHeaders 設定從 Web 服務器回應中傳回的自訂回應標頭。
redirectHeaders 設定只有在 Web 服務器重新導向要求時,才會在回應中傳回的回應標頭。

組態範例

下列程式碼範例會啟用預設網站的 HTTP keep-alives。

<configuration>
   <system.webServer>
      <httpProtocol allowKeepAlive="true" />
   </system.webServer>
</configuration>

注意

下列預設 <httpProtocol> 元素是在 IIS 7 的 ApplicationHost.config 檔案中設定。

<httpProtocol>
   <customHeaders>
      <clear />
      <add name="X-Powered-By" value="ASP.NET" />
   </customHeaders>
   <redirectHeaders>
      <clear />
   </redirectHeaders>
</httpProtocol>

範例程式碼

下列程式碼範例會啟用預設網站的 HTTP keep-alives。

AppCmd.exe

appcmd.exe set config "Default Web Site" -section:system.webServer/httpProtocol /allowKeepAlive:"True"

C#

using System;
using System.Text;
using Microsoft.Web.Administration;

internal static class Sample
{
   private static void Main()
   {
      using (ServerManager serverManager = new ServerManager())
      {
         Configuration config = serverManager.GetWebConfiguration("Default Web Site");

         ConfigurationSection httpProtocolSection = config.GetSection("system.webServer/httpProtocol");
         httpProtocolSection["allowKeepAlive"] = true;

         serverManager.CommitChanges();
      }
   }
}

VB.NET

Imports System
Imports System.Text
Imports Microsoft.Web.Administration

Module Sample

   Sub Main()
      Dim serverManager As ServerManager = New ServerManager
      Dim config As Configuration = serverManager.GetWebConfiguration("Default Web Site")

      Dim httpProtocolSection As ConfigurationSection = config.GetSection("system.webServer/httpProtocol")
      httpProtocolSection("allowKeepAlive") = True

      serverManager.CommitChanges()
   End Sub

End Module

JavaScript

var adminManager = new ActiveXObject('Microsoft.ApplicationHost.WritableAdminManager');
adminManager.CommitPath = "MACHINE/WEBROOT/APPHOST/Default Web Site";

var httpProtocolSection = adminManager.GetAdminSection("system.webServer/httpProtocol", "MACHINE/WEBROOT/APPHOST/Default Web Site");
httpProtocolSection.Properties.Item("allowKeepAlive").Value = true;

adminManager.CommitChanges();

VBScript

Set adminManager = WScript.CreateObject("Microsoft.ApplicationHost.WritableAdminManager")
adminManager.CommitPath = "MACHINE/WEBROOT/APPHOST/Default Web Site"

Set httpProtocolSection = adminManager.GetAdminSection("system.webServer/httpProtocol", "MACHINE/WEBROOT/APPHOST/Default Web Site")
httpProtocolSection.Properties.Item("allowKeepAlive").Value = True

adminManager.CommitChanges()