Handle HTTP Re-Directions in LoadRunner Scripts yourself

Sometimes you want to grab a Session-Cookie or something else from the HTTP Header’s during a redirection, but normally LoadRunner does not allow you to web_reg_save() those if there are multiple re-directions, and the same “variable” appears multiple times.

I’ve solved this problem by handling the re-directions myself in-script by doing a DO WHILE loop that keeps running till the end of re-directions, or error for that matter.

Here’s the code for a simple redirection loop (tries to go to mercury.com, but gets re-directed to hp.com instead):

	int HttpRetCode=0;

	// Set redirection depth to 0
	web_set_option("MaxRedirectionDepth", "0", LAST);

	// Initial Location where we want to go
	lr_save_string("http://www.mercury.com", "Location");

		// Save new Redirection Info (Location: header)
						   "LB=Location: ",
						   LAST );

		// do any web call (This needs to be modified to suit your needs)
		  LAST );

		// Get HTTP response Code
		HttpRetCode = web_get_int_property(HTTP_INFO_RETURN_CODE);

	} while ((HttpRetCode>=300) & (HttpRetCode<400));

	return 0;

7 thoughts on “Handle HTTP Re-Directions in LoadRunner Scripts yourself

    • Good article Joel!

      Did you know that even if one specifies web_set_user() to LoadRunner, it always does one HTTP request without the Authenticate header and then another WITH the Authenticate header.

      This leads to a situation that if you are posting a large document (HTTP POST), it gets posted twice over the wire !!!! (=not very smart ..)

  1. Hi Kiran,

    There is of course the ‘IgnoreRedirections’ argument for the web_reg_save_param function in LoadRunner.

    When IgnoreRedirections=Yes, ONLY the final page will content will be searched, and the pages leading up to that page are not. With the default IgnoreRedirections=No, ALL pages are searched for content and values can be matched on ANY of those pages. You can even use the “Ord=ALL” option as this allows for all redirections to be found

    So it turns out, the default behaviour of searching all pages leading up to your final page is a great option for matching multiple redirects, however, as in my post, this option does not allow you to set individual timing points for the redirects, something which I find very useful.

  2. For clarification the manual from v9.52 states:

    IgnoreRedirections: If “IgnoreRedirections=Yes” is specified and the server response is redirection information (HTTP status code 300-303, 307), the response is not searched. Instead, after receiving a redirection response, the GET request is sent to the redirected location and the search is performed on the response from that location.
    This attribute is optional. The default is “IgnoreRedirections=No”.

    As for the the usefulness of the parameter, it is totally dependent on the situation and type of script one is creating. I was not able to use the parameter since based on the Location I received I was supposed to go/not go there. It was a special case where a special way to handle it had to be created.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s