5 Replies Latest reply on Nov 13, 2006 3:11 PM by NUMB-LOCK

    HTTPService problem when sending xml

    jpwrunyan Level 1
      I found the following article on submitting XML data directly in place of value-pairs using HTTPService.
      Since I am working with Struts and all my data is XML to begin with, I thought this would be a nice clean easy way to do things:

      Sending XML Over HTTP

      Unfortunately, it doesn't seem to work... at all.

      I am sending to a "target.jsp" page that I *think* should just tell me what the document type of the request is. Since I am sending XML, it should say "XML":

      <%@ page import="java.io.BufferedReader,
      javax.xml.parsers.DocumentBuilderFactory,
      javax.xml.parsers.DocumentBuilder,
      org.w3c.dom.Document,
      org.xml.sax.InputSource"%>
      <?xml version="1.0" encoding="UTF-8" ?>
      <test2>
      <data>
      <%
      BufferedReader br = new BufferedReader(request.getReader());
      DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
      DocumentBuilder db = factory.newDocumentBuilder();
      Document doc = db.parse(new InputSource(br));
      out.print(doc.getDoctype()); //prints the original doc type as the value for <data>, right?
      %>
      </data>
      </test2>

      Then, in my Flex application I have the following actionscript:

      var serviceCall:HTTPService = new HTTPService();
      serviceCall.method = "POST";
      serviceCall.resultFormat = mx.rpc.http.HTTPService.RESULT_FORMAT_E4X;
      serviceCall.url = "target.jsp";
      serviceCall.contentType = "application/xml";
      serviceCall.request = new XML(<blorch><a><test></test></a></blorch>);
      serviceCall.send();
      //not shown: listeners and display logic

      Now, the above, when executed displays its result as:
      <test2>
      <data>null</data>
      </test2>

      What am I doing wrong? Why doesn't the .jsp have anything in its request??? I assume it says "null" because it doesn't have anything to begin with!
        • 1. Re: HTTPService problem when sending xml
          rkschulz
          Not sure about using JSP, but here is some code that should get you going using servlets.

          FLEX:

          var _dataLoaderHTTPService:HTTPService;

          some method () {

          _dataLoaderHTTPService = new HTTPService();
          _dataLoaderHTTPService.method="POST";
          _dataLoaderHTTPService.url=".../YourServlet";
          _dataLoaderHTTPService.useProxy=false;
          _dataLoaderHTTPService.resultFormat="text";
          _dataLoaderHTTPService.contentType="application/x-www-form-urlencoded";
          _dataLoaderHTTPService.addEventListener("result",dataLoaded);

          var params:Object = new Object();
          params.param1 =
          params.param2 =
          _dataLoaderHTTPService.send(params);
          }

          private function dataLoaded(e:Event):void {
          var xml:XML = new XML(_dataLoaderHTTPService.lastResult.toString());
          }

          JAVA

          public void doPost(HttpServletRequest request, HttpServletResponse response)
          throws IOException, ServletException {

          response.setContentType("application/xml");
          PrintWriter out = response.getWriter();
          String p1 = request.getParameter("param1");
          String p2 = request.getParameter("param2");

          out.append("<xml> ... ");

          ____

          You should be able to do the same with the JSP on the Java side.
          Hope this helps.

          R.


          • 2. Re: HTTPService problem when sending xml
            Peter Farland Level 3
            When working with HTTPServices you should use an HTTP sniffer to debug what is actually being sent on the wire... this is a more reliable test than not being sure if Java code in a JSP is working.

            Also, if you do confirm with a sniffer that nothing is being posted, instead of this:

            serviceCall.request = new XML(<blorch><a><test></test></a></blorch>);
            serviceCall.send();

            Can you try

            var data:XML = <blorch><a><test></test></a></blorch>;
            serviceCall.request = data;
            serviceCall.send();

            Or try simply:

            var data:XML = <blorch><a><test></test></a></blorch>;
            serviceCall.send(data);
            • 3. Re: HTTPService problem when sending xml
              jpwrunyan Level 1
              Pete,

              The code above was just one way I have tried sending XML. I have tried various other ways such as those you listed. I will, when I have a moment, go back and try again, though. In the meantime, not to be overly ignorant (or lazy), but could you perhaps tell me what an HTTP sniffer is or give a link describing how to do what you have suggested?
              • 4. Re: HTTPService problem when sending xml
                ntsiii Level 3
                I have heard a lot of folks complaining of problems with contentType= "application/xml";

                Note that rkschultx code does not use this content type.

                Tracy
                • 5. HTTPService problem when sending xml
                  NUMB-LOCK
                  You can also simply use the <mx:TraceTarget/> tag to spit out what is being sent and received. It even gives you the entire SOAP message. Just put <mx:TraceTarget/> as the second line of your code right under the "mx:application" line.

                  Bob I.