Search This Blog


Thursday, August 5, 2010

Stream erorr 2032 in Flex using HttpService

Lately I developed an application for a workshop I was giving. The application was a Flex application which communicated with a SpringMVC 3.0 annonation based based web application using Xstream as XML serialization library. All seemed well until in specific browsers and operation systems I got a 'Error #2032: Streamfault'. I did not found a solution on the Internet other than verifying that my URL was correct and disable HTTP caching on the server. None of these solutions worked.

When I executed this specific request in the browser, I got a HTTP 406 status code. The 406 status code says:
The resource identified by this request is only capable of generating responses with characteristics not acceptable according to the request "accept" headers ().
Inspecting the request the browser sent, showed an accept-header of '*/*'. I compared this to the accept-header FireFox sent which was: 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8'.

The solution turned out to set the accept-header explicitly on the HttpService. I used the following code for this:

var httpService:HTTPService = new HTTPService();
httpService.url = url + "/tasks";
httpService.requestTimeout = 10;
// Set the content type expected by the server
httpService.contentType = "application/xml";
// Explicitly set the accept header
httpService.headers = { accept: "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8" };
httpService.resultFormat = "e4x";

As a sidenote, to disable HTTP caching on the server, you can use the following code:

response.addHeader("Pragma", "no-cache");
response.addHeader("Cache-Control", "no-cache");
response.addHeader("Cache-Control", "no-store");
response.addHeader("Cache-Control", "must-revalidate");
response.addHeader("Expires", "Mon, 8 Aug 2006 10:00:00 GMT");