Flex Workaround: HTTPService Does Not Return Response Headers


I am surprised that HTTPService of Adobe Flex does not return response headers!  

What if an application written in Flex would want to communicate with a system written in J2EE and was using the Set-Cookie response header to get the JSESSIONID for login validation? This is what I have experienced when I started to work on a project that aims to evaluate the capability of Adobe Flex. Luckily I was able to do a workaround but I had to reuse the .Net WCF-based service I created in another project. My WCF-based service contains an OperationContract or a web method named MakeRequest. It accepts the parameters target url, request method, i.e. POST or GET, an array of header names, an array of header values, an array of parameter names, and an array of the values to post. The key to this web method is the WebClient
class from the System.Net namespace because it returns response headers together with the response body. The output string of this web method is the response body plus the response headers delimited by “@@@@@”.  

    [OperationContract]
    public string MakeRequest(string pstrTargetUrl,
                              string pstrMethod,
                              string[] pstrHeaderNames,
                              string[] pstrHeaderValues,
                              string[] pstrPostValueNames,
                              string[] pstrPostValues)
    {
        string returnString = "Bad Request";
        // This is a GET request with no form values and optional Basic auth
        if (pstrMethod.ToUpper() == "GET")
        {
            using (WebClient wc = new WebClient())
            {
                wc.Proxy = null;
                if (pstrHeaderNames != null && pstrHeaderNames.Length > 0)
                {
                    for (int i = 0; i < pstrHeaderNames.Length; i++)
                    {
                        wc.Headers.Add(pstrHeaderNames[i], pstrHeaderValues[i]);
                    }
                }
                returnString = wc.DownloadString(pstrTargetUrl);
            }
        }
        // This is a POST request with form values and optional Basic auth
        if (pstrMethod.ToUpper() == "POST" && pstrPostValueNames != null)
        {
            using (WebClient wc = new WebClient())
            {
                wc.Proxy = null;               
                if (pstrHeaderNames != null && pstrHeaderValues != null)
                {
                    for (int i = 0; i < pstrHeaderNames.Length; i++)
                    {
                        wc.Headers.Add(pstrHeaderNames[i], pstrHeaderValues[i]);
                    }
                }
                var data = new NameValueCollection();
                for (int i = 0; i < pstrPostValueNames.Length; i++)
                {
                    data.Add(pstrPostValueNames[i], pstrPostValues[i]);
                } 

                byte[] result = wc.UploadValues(pstrTargetUrl, "POST", data);
                byte[] responseHeader = wc.ResponseHeaders.ToByteArray();
               
                returnString = System.Text.Encoding.UTF8.GetString(responseHeader) + "@@@@@" +
   System.Text.Encoding.UTF8.GetString(result); 

                
            }
        }
        return returnString;
    } 

At the Flex side of the code, use the mx.rpc.soap.WebService to call the web method above with the required parameters. A sample of the header and parameter key-value pairs is shown below. Note that headerNames and headerValues should have a one-to-one correspondence, i.e. each element of the headerNames array should correspond to each element of the headerValues array. paramNames and paramValues should also have a one-to-one correspondence. 

var headerNames : Array = new Array("Content-Type");
var headerValues : Array = new Array("application/x-www-form-urlencoded");
var paramNames : Array = new Array("USER", "CLIENT_PASSWORD", "DATABASE"); var paramValues: Array = new Array("username", "password", "dbname");
About these ads
This entry was posted in Uncategorized and tagged , . Bookmark the permalink.

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 )

Twitter picture

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

Facebook photo

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

Google+ photo

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

Connecting to %s