(Apple Problem ID # 2692181)
Sometimes POST works from a Java applet using Macintosh Internet Explorer 5.0 and Apple MRJ 2.2.4 under Macintosh OS 9, and sometimes POST doesn't work. It appears from the demonstration here that the first POST completes successfully but subsequent attempts to POST fail, hanging the browser.
Until mid-July 2003 this applet posted to a CGI script and demonstrated the crashes discussed below. After some hosting changes, posting to CGI scripts is no longer used here. At some point a servlet-based test applet will be tried.
The pre-July 2003 behavior:
You can reproduce this problem as follows:
As you can see by the Java Messages and comparing that to source code below, the first POST is successful but the second POST fails in the new DataOutputStream line. The code contains no threads so this is not due to any kind of thread conflict in the programmer's code.
This applet works fine using MRJ 2.2.4 with Netscape 6, though tests were done with the updated MRJPlugin from http://homepage.mac.com/mrjplugin/. This also works with the Apple Applet Runner under Macintosh OS 9. It also works under OS X using either Applet Launcher or Internet Explorer 5.1 preview. It also works fine using Netscape 4.75 using its built-in Java Virtual Machine, but does not work using Netscape 4.75 using the MRJPlugin, as demonstrated by following this link using the EMBED tag, failing in a different way without POSTing at all and without hanging the browser.
A partial workaround for this problem using Sun's HttpURLConnection class is described at: http://developer.apple.com/qa/java/java26.html but this requires the applet to be digitally signed and it does not appear to work if the user is behind a proxy server.
If you have any insights, workarounds or comments about this test page please contact Mickey Segal. A listing of many Macintosh Java bugs with demonstration applets is at this link, including information on how to add any necessary Java plugins.
The code for this applet is available from this link and displayed below, including all the println statements used for debugging:
import java.applet.*;
import java.awt.*;
import java.net.*;
import java.io.*;
public class PostNoThread extends Applet {
Button postButton;
TextField tf;
public void init()
{
tf = new TextField(10);
add(tf);
postButton = new Button ("Type a String and then press this button");
add (postButton);
URLPoster.refreshStatics();
}
public boolean action(Event evt, Object obj)
{
if (evt.target == postButton)
{
try
{
URLPoster.numberStarted++;
new URLPoster(URLPoster.numberStarted, "TestExported", "test.txt",
tf.getText() + ": data from run " + String.valueOf(URLPoster.numberStarted));
System.out.println(URLPoster.numberStarted + " POST is done");
}
catch (Exception e)
{
System.out.println("send failed");
return true;
}
}
return true;
}
} // END OF Class PostNoThread
class URLPoster {
URL urlCGI;
String query, file, ext;
int numberOfThis;
static int numberStarted;
URLPoster(int numberOfThis, String folder, String fileWithExt, String data)
{
this.numberOfThis = numberOfThis;
urlCGI = null;
try
{
urlCGI = new URL("https://segal.org/cgi-bin/form2mail");
}
catch (Exception e)
{
urlCGI = null;
System.out.println("url could not be formed" + e);
}
int dotPosition = fileWithExt.indexOf('.');
file = fileWithExt.substring(0, dotPosition);
ext = fileWithExt.substring(dotPosition + 1);
query = URLEncoder.encode("config") + "=" + URLEncoder.encode("/templates/" + folder +
"Append.txt");
addToQuery("file", file);
addToQuery("ext", ext);
addToQuery("data", data);
System.out.println(numberOfThis + " POST starting");
post();
}
static final void refreshStatics()
{
numberStarted = 0;
}
final void addToQuery (String name, String value)
{
query += ("&" + URLEncoder.encode(name) + "=" + URLEncoder.encode(value));
}
final void post()
{
try
{
URLConnection uc = urlCGI.openConnection();
System.out.println(numberOfThis + " uc = " + uc);
uc.setUseCaches(false);
System.out.println(numberOfThis + " point A");
uc.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
System.out.println(numberOfThis + " point B");
// setRequestProperty needed since some Netscape versions don't give this out properly
uc.setAllowUserInteraction(false);
System.out.println(numberOfThis + " point C");
uc.setDoInput(true);
System.out.println(numberOfThis + " point D");
uc.setDoOutput(true); // after setRequestProperty which can reset doOutput to false
System.out.println(numberOfThis + " point E");
DataOutputStream udos = new DataOutputStream(uc.getOutputStream ());
System.out.println(numberOfThis + " point F");
udos.writeBytes(query);
System.out.println(numberOfThis + " point G");
udos.flush(); // sends
System.out.println(numberOfThis + " point H");
udos.close(); // cgi program won't return response until this is closed
System.out.println(numberOfThis + " point I");
DataInputStream udis = new DataInputStream(uc.getInputStream ());
System.out.println(numberOfThis + " point J");
// NN won't post data unless inputStream is opened, but reading input is not necessary
udis.close(); // always close after opening
System.out.println(numberOfThis + " point K");
}
catch (Exception e)
{
System.out.println("Error in posting");
}
}
} // END OF Class URLPoster