Customizing HTTP Headers with addRequestHeader()

     

Customizing HTTP Headers with addRequestHeader()

graphics/new_icon.jpg

New addRequestHeader() methods for the LoadVars class and the XML class allow you to add or change HTTP request headers . You can use these headers for a wide variety of standard purposes, such as telling the server what MIME content types (for example, text, video, audio) or character set the browser prefers or expects, or indicating what language you prefer (for servers supporting multiple languages). There are also special-purpose headers such as the SOAPAction header, which indicates the intent of a SOAP HTTP request. And you can define custom headers yourself.


Server scripting languages such as ColdFusion Markup Language (CFML), PHP, and ASP can be used to retrieve all headers. For example, in CFML you use the built-in GetHttpRequestData() function, like this:

 <cfset x = GetHttpRequestData()> 

You add the headers to the LoadVars or XML object in Flash. Methods such as send() or sendAndLoad() are used to automatically send headers to the server when the request is made.

There are two formats for the addRequestHeader() method. One provides a single header name, and a single value for that name. The other provides an array, which contains a name, a value, a name, a value, and so on for as many name /value pairs as you wish to send.

Here's an example of adding a single SOAPAction header with the value "GetLastSellingPrice" .

 myLoadVars.addRequestHeader("SOAPAction", "GetLastSellingPrice"); 

Here's an example with two name/value pairs in an array. The first header, Content-Type, is a standard one. The second, X-ClientAppType, is a custom header, which you would have to write a program to capture, interpret, and use on the server. This example creates an array named myHeaders and then passes the array as an argument to addRequestHeader() .

 var myHeaders = ["Content-Type", "text/plain", "X-ClientAppType", "Flash"]; myXML.addRequestHeader(myHeaders); 

If, before making the request to the server, you add the same header name more than once, only the last one counts.

Primarily for security reasons, you cannot use addRequestHeader() to add or change some standard HTTP headers . They include Accept-Ranges, Age, Allow, Allowed, Connection, Content-Length, Content-Location, Content-Range, ETag, Host, Last-Modified, Locations, Max-Forwards, Proxy-Authenticate, Proxy-Authorization, Public, Range, Retry-After, Server, TE, Trailer, Transfer-Encoding, Upgrade, URI, Vary, Via, Warning, and WWW-Authenticate.



Using Macromedia Studio MX 2004
Special Edition Using Macromedia Studio MX 2004
ISBN: 0789730421
EAN: 2147483647
Year: N/A
Pages: 339

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net