如何取代 URL 要求中的參數How to replace parameters in a URL request

重要

已不再支援此版本的 Operations Manager,建議升級至 Operations Manager 2019This version of Operations Manager has reached the end of support, we recommend you to upgrade to Operations Manager 2019.

當您使用 Web 應用程式編輯器來擷取 Web 應用程式時,它可能會在一或多個要求中,包含會在每次連線到該應用程式時變更的唯一資訊。When you capture a web application by using the Web Application Editor, it can include unique information in one or more requests that changes each time you connect to the application. 此資訊通常會包含在針對要求的回應中,然後由一或多個後續要求所使用。This information is typically included in the response to a request and then used by one or more subsequent requests.

例如,應用程式可能會在使用者登入時建立唯一的工作階段識別碼。For example, an application might create a unique session ID when a user logs on. 此工作階段識別碼必須在登入程序之後包含在每個要求中。This session ID must be included in each request after the logon process. 在沒有正確工作階段識別碼的情況下,這些要求都會失敗。Without the correct session ID, each of these requests fails. 由於您在系統執行第一個要求之前並無法知道此值是什麼,因此無法將它明確地包含在要求的設定中。Because you do not know what this value is until the first request is run, it cannot be explicitly included in the configuration of the request. 如果您是透過記錄瀏覽器工作階段來建立 Web 應用程式,便可在每個要求的 URL 中收集工作階段識別碼。If you create the web application by recording a browser session, the session ID is collected in the URL of each request. 不過在該應用程式執行時,要求會失敗,因為工作階段識別碼會具有和已記錄的工作階段識別碼不同的值。However, when the application is run, the requests fail because the session ID will have a value that is different from the recorded session ID.

若要設定這類應用程式,您可以從某個要求的回應主體中擷取內容參數,然後在一或多個後續要求中使用該參數的值。To configure such an application, you can extract a context parameter from the body of the response of one request and use the value of that parameter in one or more subsequent requests. 您接著會將後續要求中的明確值取代為代表該參數的變數。You then replace the explicit value in subsequent requests with a variable that represents the parameter. 每次執行綜合交易時,系統會在定義參數的要求中填入該參數。Each time the synthetic transaction is run, the parameter is populated in the request where it is defined. 當變數用於後續要求中時,系統會在將要求傳送至應用程式之前,以已收集的值來取代它。When the variable is used in the subsequent requests, it is replaced with the collected value before the request is sent to the application.

單一應用程式可以使用任何數目的內容參數。A single application can use any number of context parameters. 任何數目的要求都可以使用單一參數,但必須在定義參數所在的要求之後執行。Any number of requests can use a single parameter but must be run after the request where the parameter is defined.

工作階段識別碼範例Session ID example

以應用程式會在使用者登入時建立工作階段識別碼的情況為例。Consider the example where an application creates a session ID when a user logs on. 登入頁面之後的每個要求中都需要此工作階段識別碼。This session ID is required in each request after the logon page. 若要實作此案例,您必須在系統首次產生工作階段識別碼時擷取它,然後在每個後續要求中使用該值。To implement this scenario, you have to capture the session ID when it is first generated, and then use that value in each subsequent request.

一開始,您會使用在如何擷取 Web 應用程式記錄 (機器翻譯) 中所述的程序來擷取登入和後續動作。You start by using the process described in How to Capture Web Application Recording to capture the logon and subsequent actions. 登入應用程式並執行某些動作的已記錄工作階段看起來可能類似下列範例。The recorded session for logging on to the application and performing some actions might look similar to the following example.


http://www.myapp.com/home.aspx

http://www.myapp.com/search.aspx?query=testing&sessionid=32793279321721

http://www.myapp.com/results.aspx?sessionid=32793279321721

http://www.myapp.com/submit.aspx?sessionid=32793279321721

在此要求序列中,工作階段識別碼是由第一個要求所建立,然後用於第二、第三及第四個要求中。In this request sequence, the session ID is created by the first request and used in the second, third, and fourth requests. 執行此監視將會失敗,因為第一個要求會產生新的工作階段識別碼,而這組識別碼與擷取工作階段時所使用的那組工作階段識別碼並不相符。When you run this monitor, it fails because the first request generates a new session ID that could not match the session ID that was used when the session was captured.

若要搭配參數取代項目來設定此要求序列,您必須在第一個要求上建立擷取規則,以建立適用於工作階段識別碼的內容參數。To configure this request sequence with parameter replacement, you have to create an extraction rule on the first request to create a context parameter for the session ID. 擷取規則會檢查要求主體,以找出工作階段識別碼變數的值。The extraction rule inspects the body of the request to locate the value for the sessionid variable. 您接著會修改後續要求以使用此參數,而非使用工作階段識別碼的值。You would then modify the subsequent requests to use this parameter instead of the value for the session ID.

已修改的要求外觀應與下列範例類似。The modified requests look similar to the following example.


http://www.myapp.com/home.aspx

http://www.myapp.com/search.aspx?query=testing&sessionid=$ParametersContext/sessionID$

http://www.myapp.com/results.aspx?sessionid=$ParametersContext/sessionID$

http://www.myapp.com/submit.aspx?sessionid=$ParametersContext/sessionID$

建立擷取規則Creating an extraction rule

內容參數是由「擷取規則」 收集,而每個擷取規則都會收集單一內容參數。Context parameters are collected by an extraction rule, and each extraction rule collects a single context parameter. 您會在最初產生所需資料之要求的 [內容] 對話方塊中建立擷取規則。You create an extraction rule in the Properties dialog box of the request that initially generates the required data. 若要識別要擷取的值,您必須檢視從特定要求傳回的回應主體。To identify the value to extract, you must view the body of the response returned from the particular request. 您可以檢視在瀏覽器中傳回之頁面的原始碼,或是使用能讓您檢查 HTTP 回應詳細資料的工具。You can either view the source of the page returned in the browser or use a tool that lets you inspect the details of HTTP responses. 您無法使用 Web 應用程式編輯器來檢視文字。You cannot view the text by using the Web Application Editor.

當您識別出包含需擷取之資訊的要求時,您可以在該要求的內容中檢視 [擷取規則] 索引標籤,然後建立一或多個擷取規則。When you have identified the request that contains the information you have to extract, you view the Extraction Rules tab in the properties of that request and create one or more extraction rules. 下表顯示每個擷取規則的詳細資料。The details of each extraction rule are shown in the following table.

選項Option DescriptionDescription
內容參數名稱Context parameter name 輸入要授與內容參數的名稱。Enter the name to give the context parameter.
開始於Starts with 在回應主體中輸入能識別參數值開頭的文字。Enter the text in the body of the response that identifies the start of the parameter value. 您應該指定足夠的字元,以確保該字串是唯一的。You should specify enough characters to ensure that the string is unique. 參數的值會緊接在最後一個指定的字元之後開始。The value for the parameter starts immediately after the last specified character.
結束值Ends with 在回應主體中輸入能識別參數值結尾的文字。Enter the text in the body of the response that identifies the end of the parameter value. 參數的值會緊接在第一個指定的字元之前結束。The value for the parameter ends immediately before the first specified character.
索引Index 如果 [開始值] 方塊中的文字出現超過一次,此值將會指出要使用哪一個值。If the text in the Starts with box occurs more than one time, this value indicates which value to use. 如果該文字只出現一次,或是它第一次出現時便會顯示要擷取的文字,則此值應為 0。If the text only appears one time, or the first occurrence of it shows the text to extract, the value should be 0. 如果應該擷取第二個值,則此值應為 1,依此類推。If the second value should be extracted, the value should be 1, and so on.
搜尋相符文字時忽略大小寫Ignore case during search for matching text 指定系統是否應該忽略 [開始值] 和 [結束值] 方塊所搜尋之字元的大小寫。Specifies whether to ignore the case of the characters being searched by the Starts with and Ends with boxes.
執行擷取字串的 URI 編碼Perform URI encoding of extracted strings 指定是否要在收集已擷取字串之後將它編碼。Specifies whether to encode the extracted string after it is collected.

將參數插入要求中Inserting a parameter into a request

若要在要求中使用參數,您必須將明確值取代為代表參數的變數。You use a parameter in a request by replacing the explicit value with a variable representing the parameter. 變數的格式是 $ParametersContext/<ContextParameterName>$。The format of the variable is $ParametersContext/<ContextParameterName>$. 執行要求時,系統會將變數取代為由參數所擷取的資料。When the request is run, the variable is replaced with the data extracted by the parameter.

您可以使用下列其中一種方法來將變數插入要求中:You can insert the variable into the request by using one of the two following methods:

  • 在 [要求內容] 對話方塊中,按一下 [一般] 索引標籤,然後按一下 [要求 URL] 來修改要求的要求 URL。In the Request Properties dialog box, click the General tab, and then click Request URL to modify the request URL for the request.
  • 在 [要求內容] 對話方塊中,按一下 [一般] 索引標籤,然後按一下 [插入參數] 按鈕。In the Request Properties dialog box, click the General tab, and then click the Insert parameter button. 針對要求使用 [插入參數] 對話方塊。Use the Insert Parameter dialog box for the request. 這可從要求的 [要求內容] 對話方塊中的 [一般] 索引標籤上的 [插入參數] 按鈕存取。This is accessed from the Insert parameter button on the General tab in the Request Properties dialog box for the request.

使用參數擷取的範例 Web 應用程式Sample web application Using parameter extraction

下列程序會提供在 Web 應用程式中使用參數擷取的範例。The following procedure provides an example of using parameter extraction in a web application. 此範例會在 Bing 首頁的 [目前熱門] 區段中針對第一個項目執行查詢。This example performs a query for the first entry in the Popular Now section of the Bing home page. 由於此值會經常變更,您必須先連線至主頁面,並從回應主體收集搜尋字詞。Because this value changes regularly, you have to first connect to the main page and collect the search term from the body of the response. 您接著會使用此字詞來建置要求,以執行實際的搜尋。You then use this term to build the request to perform the actual search.

下面會顯示 Bing 的主頁面,並醒目提示 [目前熱門] 區段。The main Bing page is shown below with the Popular Now section highlighted.

Bing 首頁

若要判斷搜尋字詞是出現在回應本文的哪個位置,您可以檢視頁面的原始碼。To determine where in the response body the search term appears, you can view the source of the page. 下面會顯示一部分的原始碼,其中包含 [目前熱門] 區段的 HTML 程式碼。A portion of the source is shown below with the HTML code of the Popular Now section. 在此 HTML 程式碼中,您只需要下圖中醒目提示的搜尋字串。In this HTML code, you only need the search string which is highlighted in the following illustration. 要求是由 http://www.bing.com ,並在後面接著此字串所組成。The request is formed from http://www.bing.com followed by this string.

Bing [目前熱門] 搜尋字串

您可以直接提取字詞本身,但將整個字串都包含在參數中是比較直覺的做法。You could just pull out the term itself, but it is more straightforward to include the entire string in the parameter. 此字串的前面會有 <h3>目前熱門</h3><ul><li><a href=> 字元,並以下一次出現的 " 作為結尾。This string is preceded by the characters <h3>Popular now</h3><ul><li><a href=> and ends with the next occurrence of ". 那些值便是您在定義參數擷取時所會使用的值。Those are the values that you will use when you define the parameter extraction.

記錄範例 Web 應用程式To record a sample web application

  1. 使用如何擷取 Web 應用程式記錄 (機器翻譯) 中的程序來記錄 Web 應用程式。Use the procedure in How to Capture Web Application Recording to record a web application.
  2. 記錄時,請連線至 http://www.bing.com While recording, connect to http://www.bing.com.
  3. 您也可以選擇性地使用瀏覽器上的選項來檢視 Bing 首頁的原始碼,然後找出 HTML 程式碼的 [目前熱門] 區段。Optionally, use the option on your browser to view the source of the Bing home page and locate the Popular Now section of the HTML code.
  4. 按一下 [目前熱門] 底下的第一個搜尋字詞。Click the first search term under Popular Now.
  5. 儲存 Web 應用程式的記錄。Save the recording to the web application.
  6. 將最後一個要求移除,因為它不是必要的。Remove the last request because this is not required. 若要移除最後一個要求,請選取該要求,然後按一下 [動作] 窗格中的 [刪除]。To remove the last request, select the request, and then click Delete in the Actions pane. 產生的要求看起來應該如下列 URL 所示:The resulting requests should look similar to the following URLs:

已記錄的 Bing Web 應用程式

建立擷取規則To create an extraction rule

  1. 選取第一個要求,然後按一下 [動作] 窗格中的 [內容]。Select the first request, and then click Properties in the Actions pane.

  2. 選取 [擷取規則] 索引標籤。Select the Extraction Rules tab.

  3. 按一下 [新增] 。Click Add. [新增擷取規則] 對話方塊隨即開啟。The Add Extraction Rule dialog box opens.

  4. 在 [新增擷取規則] 對話方塊中,於 [內容參數名稱] 方塊內,輸入 SearchStringIn the Add Extraction Rule dialog box, in the Context parameter name box, type SearchString.

  5. 在 [開始值] 方塊中,輸入 <h3>目前熱門</h3><ul><li><a href=>In the Starts with box, type <h3>Popular now</h3><ul><li><a href=>.

  6. 在 [結束值] 方塊中,輸入 "In the Ends with box, type ". 擷取規則看起來應與下圖類似。The extraction rule should look similar to the following illustration.

    擷取規則

  7. 按一下 [確定] 以儲存並關閉擷取規則。Click OK to save and close the extraction rule.

  8. 按一下 [確定] 以儲存並關閉要求。Click OK to save and close the request.

將參數插入要求中To insert a parameter into a request

  1. 選取第二個要求,然後按一下 [動作] 窗格中的 [內容]。Select the second request, and then click Properties in the Actions pane.

  2. 在 [一般] 索引標籤上,按一下 [插入參數] 。On the General tab, click Insert parameter.

  3. 在 [字串] 方塊中,刪除 www.bing.com/ 之後的所有文字。In the String box, delete all text after www.bing.com/.

  4. 將游標置於 URL 結尾處,www.bing.com 的正後方,選取 [參數] 方塊中的 [SearchString],然後按一下 [插入]。With the cursor positioned at the end of the URL, just after www.bing.com, select SearchString in the Parameters box, and then click Insert. 這會插入 $ParametersContext/SearchString$ 這個變數。This inserts the variable $ParametersContext/SearchString$. 最後的要求看起來會與下圖類似。The final request looks similar to the following illustration.

    插入參數

  5. 按一下 [確定] 以關閉對話方塊。Click OK to close the dialog box.

  6. 按一下 [確定] 以儲存並關閉要求。Click OK to save and close the request. 修改過的要求序列看起來應與下圖類似。The modified request sequence should look similar to the following illustration.

    已修改的 Bing Web 應用程式

  7. 按一下 [套用] 以套用變更,然後關閉 [Web 應用程式編輯器] 。Click Apply to apply the changes, and then close the Web Application Editor.