Admittedly, this borders on being off-topic (since its more about URLConnections
than about servlets), but there are servlets lurking around on the periphery so
hopefully you'll bear with me.  :)

I'm communicating with servlets on a remote machine using URLConnections.
Specifically, I use the following method where urlString points to a servlet
and formData contains the name/value pairs which are posted to the servlet.

========
  private static InputStream getInputStream(String urlString, Properties formData )
    throws java.net.MalformedURLException, java.io.IOException {

    // Build the content to be posted
    ByteArrayOutputStream content = new ByteArrayOutputStream();
    String theName = "";
    String theValue = "";
    String connector = "";
    if (formData != null) {
      for (Enumeration e = formData.propertyNames(); e.hasMoreElements();) {
        theName = (String)e.nextElement();
        theValue = URLEncoder.encode(formData.getProperty(theName));
        StringUtil.write(content,connector + theName + "=" + theValue);
        connector = "&";
      }
    }
    byte[] content_bytes = content.toByteArray();
    int content_length = content_bytes.length;
    content.close();

    // Post the content to the URL.
    URL url = new URL(urlString);
    URLConnection urlConnection = url.openConnection();
    urlConnection.setAllowUserInteraction(true);
    urlConnection.setDoInput(true);
    urlConnection.setDoOutput(true);
    urlConnection.setRequestProperty("User-Agent","MyUserAgent");
    
urlConnection.setRequestProperty("Content-type","application/x-www-form-urlencoded");
    urlConnection.setRequestProperty("Content-length", String.valueOf(content_length));

    // Grab the OutputStream.
    OutputStream outputStream = urlConnection.getOutputStream();
    outputStream.write(content_bytes);
    outputStream.close();

    // Return the InputStream
    return urlConnection.getInputStream();   // This is line 162 in NetUtil.java (see 
below)
  }
=============

Most of the time (more than 99%), this works just fine.  However, occasionally,
this method throws a FileNotFoundException at the last line where it tries
to get the InputStream from the urlConnection.  For example:

java.io.FileNotFoundException: http://the.remote.host/servlets/RemoteServlet
        at 
sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:485)
        at ImageCafe.lib.NetUtil.getInputStream(NetUtil.java:162)
        at ImageCafe.lib.NetUtil.postRequest(NetUtil.java:82)
        ...


First of all, I'm confused because this is happening in a place in the code after I've
already successfully written to an OutputStream connected to the RemoteServlet
so I didn't expect there to be problems when I grab the resulting InputStream.
I'm hoping that someone with a better understanding of URLConnections could
explain what might be going on behind the scenes which would cause this.

Secondly, if it does turn out that there is nothing that I can do to prevent this
from happening, what might I do to recover gracefully?  Should I catch the
exception and try again (say 3 times).  Might it help to throw in a Thread.sleep?

Any suggestions would be greatly appreciated.

Thanks,
John

___________________________________________________________________________
To unsubscribe, send email to [EMAIL PROTECTED] and include in the body
of the message "signoff SERVLET-INTEREST".

Archives: http://archives.java.sun.com/archives/servlet-interest.html
Resources: http://java.sun.com/products/servlet/external-resources.html
LISTSERV Help: http://www.lsoft.com/manuals/user/user.html

Reply via email to