Click to See Complete Forum and Search --> : URLConnection and Netscape
Merv
February 4th, 2002, 08:34 AM
Greetings, I have a Java Applet that sends data back to a filter DLL on a web server with the following basic format, http://server/filter.dll?Variable=Data
When I do a URLConnection with IE all is fine. I can get input and output streams and data goes back and forth.
When I use netscape to connect to the URL, Netscape throws an exception along the lines of "The requested page contained no data"
Anyone have any ideas as to why? Does the Capabilities API need to be used to make a URL connection in Netscape? Maybe a data format issue?
Norm
February 4th, 2002, 09:29 AM
Now sure about where the error in NS occurs. Is it when the applet is in the process of sending the data or after that when the applet is waiting for the response from the server.
How does the browser get involved in the applet's commmunications?
With NS, does the server receive the data? Does it then send a response back to the applet?
If the applet in the HTML page is using a URLConnection, then what does "use netscape to connect to the URL" mean?
From what you've said, I would think that the communications would be: browser sends URL to server for HTML page containing applet; server sends that page to browser; applet executes: creates URLConnection and sends data to server and receives response from server.
Where does the "contained no data" exception come in?
Norm
Merv
February 4th, 2002, 11:22 AM
The applet is running on an HTML page and using a URLConnection to another URL to send/receive data. This 'other' URL is actually a filter DLL on the server.
What is happening on the communications end is: applet sends data to the server using an output stream through a URLConnection, the applet then waits for the server to respond by checking
the input stream over the same URLConnection. This scenario is repeated as needed.
What happens is the applet creates a URL, does an openConnection and sets the properties, and then does a connect. (See Below)
The connect is where Netscape throws the exception. IE runs fine.
startURL = new URL("http", host, 80, "Filter.Dll?x=data");
hostURLConnection = startURL.openConnection();
hostURLConnection.setDoOutput(true);
hostURLConnection.setDoInput(true);
hostURLConnection.setAllowUserInteraction(true);
hostURLConnection.setUseCaches(false);
hostURLConnection.connect();
//After this the input and ouput streams are created
//and the transaction is performed.
Norm
February 4th, 2002, 12:46 PM
I compiled and excuted the code you posted and did not get the error you describe! I put the code on my test server, brought up a NS browser and entered the URL for the applet page, the HTML page and applet were returned to the browser and then no exception?
Could you post code that actually executes as you say it does?
Here's what I used:
import java.applet.*;
import java.net.*;
public class AppletConnect extends Applet{
public void init() {
String host = getCodeBase().getHost();
try {
URL startURL = new URL("http", host, 80, "Filter.Dll?x=data");
URLConnection hostURLConnection = startURL.openConnection();
hostURLConnection.setDoOutput(true);
hostURLConnection.setDoInput(true);
hostURLConnection.setAllowUserInteraction(true);
hostURLConnection.setUseCaches(false);
hostURLConnection.connect();
}catch (Exception ex) {
ex.printStackTrace();
}
//After this the input and ouput streams are created
//and the transaction is performed.
}
} // end class
Norm
Merv
February 5th, 2002, 06:54 PM
OK Norm...Sorry for the delay, but I was able to throw an applet together that seems to fail, alas not with the exact same error I saw in the real code. (The problem with using the real code is there is a large backend system needed to make it work, but this should do I think.)
Anyway, this applet will run in IE, but never does anything in Netscape...Maybe you can see something I am missing. I simply renamed a gif file to Fake.DLL so I can get past the filenotfound exception which appears to work.
Also, New InputStream is commented out as this makes IE die as well, I assume it is due to the Fake Dll file.
By the way, I really appreciate your time in looking into this with me. Now on to the code....
HTML PAGE:
<HTML>
<META HTTP-EQUIV="expires" VALUE="Tue, 23 Jun 2000 01:00:00 GMT">
<HEAD>
</HEAD>
<BODY BGCOLOR="ffffff">
<APPLET
code=AppletConnect.class
name=AppletConnect
width=350
height=290 VIEWASTEXT id=Applet1>
</APPLET>
</BODY>
</HTML>
import java.applet.*;
import java.awt.*;
import java.net.*;
import java.io.*;
public class AppletConnect extends Applet implements Runnable
{
public static java.awt.Panel mainPanel = new java.awt.Panel();
public static java.awt.TextArea myTextArea = new java.awt.TextArea("",0,0,TextArea.SCROLLBARS_VERTICAL_ONLY);
java.awt.GridBagConstraints mainConstraints = new java.awt.GridBagConstraints();
public static String host;
public static String strDllPath;
public static URLConnection hostURLConnection = null;
public static URL startURL = null;
public static DataOutputStream urlOutStream= null;
public static DataInputStream urlInStream= null;
public void init()
{
//Find out who served us the applet
host = getCodeBase().getHost();
//{{SETUP GUI
setLayout(new GridBagLayout());
setSize(357,240);
mainPanel.setLayout(null);
mainConstraints.gridx = 0;
mainConstraints.gridy = 0;
mainConstraints.gridwidth = 1;
mainConstraints.gridheight = 1;
mainConstraints.weightx = 1.0;
mainConstraints.weighty = 1.0;
mainConstraints.anchor = java.awt.GridBagConstraints.CENTER;
mainConstraints.fill = java.awt.GridBagConstraints.BOTH;
mainConstraints.insets = new Insets(50,0,0,0);
mainConstraints.ipadx = 0;
mainConstraints.ipady = 0;
mainPanel.setBackground(java.awt.Color.gray);
mainPanel.setBounds(1,0,350,225);
add(mainPanel,mainConstraints);
myTextArea.setEditable(false);
myTextArea.setBounds(5,21,342,120);
myTextArea.setForeground(java.awt.Color.black);
myTextArea.setBackground(java.awt.Color.white);
mainPanel.add(myTextArea);
myTextArea.setText("");
}//End init()
public void start()
{
strDllPath = getCodeBase().getFile();
myTextArea.append("Connecting to server...\r\n");
send("this_is_a_test");
}//End start()
/****************** SEND ROUTINE******************/
public static void send(String output)
{
String strMessageString;
String strOutput;
strMessageString = strDllPath + "ha.dll?data=" + output;
strOutput = strMessageString;
try
{
startURL = new URL("http",host,80,strOutput);
myTextArea.append("Start Connection....\r\n");
hostURLConnection = startURL.openConnection();
myTextArea.append("Open....\r\n");
hostURLConnection.setDoOutput(true);
myTextArea.append("Do Out....\r\n");
hostURLConnection.setDoInput(true);
myTextArea.append("Do In....\r\n");
hostURLConnection.setAllowUserInteraction(true);
myTextArea.append("Allow....\r\n");
hostURLConnection.setUseCaches(false);
myTextArea.append("Caches....\r\n");
try
{
hostURLConnection.connect();
}
catch(java.io.IOException e)
{
myTextArea.append("Connect Failed....\r\n");
}
myTextArea.append("Connected....\r\n");
if (null == urlOutStream)
{
myTextArea.append(hostURLConnection.toString());
myTextArea.append("Get Output Stream ....\r\n");
try
{
urlOutStream = new DataOutputStream(hostURLConnection.getOutputStream());
}
catch(Exception e){
myTextArea.append("Get Output Stream failed....\r\n");
displayExceptionMsg(e);
}
myTextArea.append("Get Output Stream passed....\r\n");
urlOutStream.flush();
}
if (null == urlInStream)
{
myTextArea.append("Get Input....\r\n");
try
{
//urlInStream = new DataInputStream(hostURLConnection.getInputStream());
}
catch(Exception e)
{
myTextArea.append("Get Input Stream Failed....\r\n");
displayExceptionMsg(e);
}
myTextArea.append("Get Input Stream passed....\r\n");
}
}
catch(Exception ex)
{
myTextArea.append("GeneralException");
displayExceptionMsg(ex);
}
}//end send
//--------------------------------------------------------------------------
// Handles all Exception Message output
//--------------------------------------------------------------------------
public static void displayExceptionMsg(Exception e)
{
String strExceptionMessage;
strExceptionMessage = e.getMessage();
if (null == strExceptionMessage)
myTextArea.append("Exception Occurred: No Message Available");
else
myTextArea.append("Exception Occurred: " + strExceptionMessage);
}//end displayExceptionMsg
public void stop()
{
}//end Stop()
public void destroy()
{
}// end Destroy()
public void run()
{
}//End run()
};
Norm
February 6th, 2002, 06:32 PM
Yes, there are many differences between IE and NS. Your code shows yet another.
Have you looked in the browser's Java console. For NS there is an error message there.
Your program is coded to do a POST(setDoOutput) but when you connect() NS issues a GET to the server?? IE doesn't do anything until some data is written, then it sends a POST.
Reading the doc for connect says that its up to the implementation on when the actual connection to the server is done. That's what you've observed.
If you really want to do a POST, remove the connect and let the flush send your request to the server.
The filenotfound exception is what the browser gives you when it gets a 40x error.
Norm
codeguru.com
Copyright WebMediaBrands Inc., All Rights Reserved.