开发者

Java servlets and receiving XML from Flash's xml.sendAndLoad() function

All,

I'm working on a Flash application that's supposed to send XML data to a Java Servlet.

I'm responsible for the Flash app; another team is responsible for the Java Servlet.

The problem we're having is that I'm familiar with Flash but not Java and Servlets; the other team is expert in Java and Servlets, but unfamiliar with Flash.

Anyway, I have some AS2 code that uses sendAndLoad() to send XML data to a server.

It works great when I send it to PHP, or ASP, or ASP.net (stuff I'm familiar with).

However, the Java team is having trouble receiving the information with their servlet.

One of the developers sent me a log entry:

GET /portal/delegate/ParticipantService?svc=someServiceName&XMLStr=[the encoded xml I sent] 

As I understand it, xml.sendAndLoad uses POST, not GET, so I don't understand why this shows up in the log as a GET. Any ideas or explanation?

Also, any suggestions about what to tell the developers about how to receive the XML?

Obviously, it's possible that the issue is with my Actionscript code, but as I said, it works if I send it to a PHP page, where I pick it up with something like this:

$doc = new DomDocument();
$doc->loadXML(file_get_contents("php://input"));

UPDATE:

Here's开发者_JS百科 what an Adobe Technote says about this:

When loadVariables or getURL actions are used to send data to Java servlets it can appear that the data is being sent using a GET request, when the POST method was specified in the Flash movie.

This happens because Flash sends the data in a GET/POST hybrid format. If the data were being sent using a GET request, the variables would appear in a query string appended to the end of the URL. Flash uses a GET server request, but the Name/Value pairs containing the variables are sent in a second transmission using POST. Although this causes the servlet to trigger the doGet() method, the variables are still available in the server request.

So, I guess what I need to know is how to tell the Java team to look for and capture the XML that's been sent...

Many thanks in advance!


Flash is doing something really weird. It may not be illegal but it's very unconventional. Servlet doesn't expect this for sure. It includes a message body in GET request. Basically, it's doing a POST with GET. Servlet ignores message body for GET.

To handle this, Java servlet has to process the message body. It's not hard to do. Just read the InputStream and convert it into a string. It's in x-www-urlencoded format.

Here is the code I use to parse it,

byte[] buffer = new byte[4096];
InputStream is = request.getInputStream();
ByteArrayOutputStrem os = new ByteArrayOutputStream();

for (int read = is.read(buffer); read > 0; read = is.read(buffer)) {
    os.write(buffer, 0, read);
}

is.close();
os.close();

String queryString = os.toString("utf-8");

...

       Map<String, String> map = new TreeMap<String, String>();

        if (queryString == null)
                return map;

        // Split at & and ignore space/newline at the ends
        String pairs[] = queryString.split("[&\\s]");

        for (String pair : pairs) {
            int pos = pair.indexOf('=');
            String key;
            String value;
            try {
                    if (pos == -1) {
                        key = URLDecoder.decode(pair, "UTF-8");
                        value = null;
                    } else {
                                        key = URLDecoder.decode(pair.substring(0, pos), "UTF-8");
                                value = URLDecoder.decode(pair.substring(pos+1, pair.length()), "UTF-8");
                                }
            } catch (UnsupportedEncodingException e) {
                                        // All JRE has UTF-8
                                throw new IllegalStateException("No UTF-8");
                        }
            map.put(key, value);
        }

        return map;

map contains the parameters included in the message body.


I think that if the request log shows the http request as a GET, 99 times out of 100 you are truly sending a GET and it's not the log that is incorrect.

As to why they might be seeing problems: I would suspect that there are certain entities in the XML content that are not being encoded properly for transport, and their XML parsing is failing on these - but you haven't exactly specified what type of problems are happening on the Java side.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜