Developer

Send XML without XMLHTTP

See how to create a portable Web application that can send XML without relying on XMLHTTP. The Java applet RSProxy will do the trick--with a little modification, of course.


This article was originally published in the Builder.com Web Development Zone e-newsletter.

Microsoft provides the XMLHTTP object for sending and receiving XML data from the client. However, this restricts you from creating portable Web applications, since the XMLHTTP object is an ActiveX object.

Most of the functionality in the XMLHTTP object lies in its ability to send and receive HTTP requests without navigating from the current Web page, giving the appearance of a connected application. This also reduces the amount of network traffic by eliminating the need to reload all the user interface information.

So how can you gain the benefits of the XMLHTTP object and still retain portability? Design Time Controls (DTCs) offer the solution. When you install Visual Interdev 6.0, it provides a set of DTCs that allow you to create everything from buttons to recordset objects. One of the DTCs is the remote scripting client. This is an applet called RSProxy, with the .java source code located in the ScriptLibrary folder. The RSProxy class utilizes the Java network class URL.

The URL class exposes methods that allow the applet to create a request to the server and receive a response, making it available for scripting inside the Web page. The RSProxy class doesn't provide a method for sending data through a POST, but with just a little tweaking, you can add this functionality. Let's run through the steps to create this functionality inside the RSProxy class.

Demo code
You can download the full source code for the following example here. This .zip file contains the .class, .java, and the two .asp files needed to run this example.

The technique
First, in the declarations section of the RSProxy class, a POST data index variable called REQUEST_POSTDATA needs to be added, and the element count must be incremented by 1. Set the C_REQUEST variable in the Request Object Definition section in the declarations to 8 instead of 7. Add this line of code right before the C_REQUEST declaration:
static final int REQUEST_POSTDATA = 7;

Next, in the newRequest object constructor method, add a String argument called post_data to the constructor. The newRequest constructor should appear like this:
protected Object[] newRequest(String requestID,String url_context,String
url,String post_data,int mode)


Set the request[REQUEST_POSTDATA] object in the request object array to the post_data argument:
request[REQUEST_POSTDATA] = post_data;

Finally, add a String argument again called post_data to the startRequest constructor:
public void startRequest(String requestID,String url_context,String url,String
 post_data,int mode)


Make sure you add the post_data argument to the newRequest call inside startRequest.

Now, you need to add the functionality to write the POST data to the output stream of the URLConnection object. Inside the runUrlDownloadThread method, replace the following code with the code in Listing A:
URL contextURL = new URL((String)request[REQUEST_URLCONTEXT]);
InputStream stream =
    new URL(contextURL,(String)request[REQUEST_URL]).openStream();


Finally, you need to import the OutputStream and URLConnection classes by adding the following code in the import section of the .java file:
import java.net.URLConnection;
import java.io.OutputStream;


(I used the Java 1.4 compiler to create the RSProxy.class file.) Add your class file to the virtual directory on your Web server. To use this applet within your Web page, add this APPLET tag:
<APPLET name=proxy codebase='/Relative/Directory/To/Class' code=RSProxy.class
height=0 width=0 VIEWASTEXT></APPLET>


Add the JavaScript code in Listing B to your event handler function to send data back to the server and receive a response.

This code sends text data from a TEXTAREA element called txtXML as POST data. The XMLPage.asp page will handle the XML data and return a response that will be stored in the value of the txtOutput TEXTAREA on the page. Now you can load the response information into a custom tag and use DOM methods to access the elements in your XML.

More information
Instead of using cookies or hidden input boxes, you can use XMLHTTP objects to access client-side ASP variables. This article explains how to create the object, set its properties, and invoke the send method.

 

Editor's Picks