Here is a code snipet that I'm using that works:
The code is used in a client side Bean from within
an applet.

The only difference I see is that I call
 setAllowUserInteraction() on the connection and use
writeBytes(buf) directly on the ouput stream object returned
from UrlConnection.getOutputStream();

Good Luck
Chris. :-)


System.out.println("-- Built cmdstr = " + cmdstr );

  // UUENCODE the varibale value list pairs
  cmdstr = URLEncoder.encode(cmdstr);
  System.out.println("-- Encoded cmdstr = " + cmdstr );

  // build the post request now
  URL  url         = new URL("http://" + commandUrlSpec + "/servlet/" +
functionTarget );



  System.out.println("-- Created URL  Obj from command str: " + url.toString()
);

  try
  {
   // execute the request and get the response from
   // the server
   URLConnection urlConnection = url.openConnection();
   urlConnection.setDoOutput(true);
   urlConnection.setDoInput(true);
   urlConnection.setAllowUserInteraction(false);

   // send the data
   DataOutputStream out = new DataOutputStream(urlConnection.getOutputStream());

   out.writeBytes(cmdstr);
   out.close();

   // retreive the response
   //DataInputStream urlData = new
DataInputStream(urlConnection.getInputStream());
   BufferedReader urlData = new BufferedReader(new
InputStreamReader(urlConnection.getInputStream()));


   System.out.println("  got response from sever " );

   // the first line tagged with "RESPONSE%" which is the
   // return code from the server function
   String data ;
   boolean bResponseFound = false;
   while (( data = urlData.readLine()) != null)
   {
    // if "#RESPONSE#" tag found process it and set all operations after
    // that to build the result set
    if(data.startsWith("#RESPONSE#$"))
    {

       bResponseFound = true;
       break;

    }// end if

   } // end while

   urlData.close();
   // if

John Baker wrote:

> Hiya,
>
> I've been messing about with firewall tunneling recently, but am having
> massive problems with URLConnection. It seems that there is one combination
> of sequence of events and streams used which actually makes it work.. take
> the Post method for example.. the online docs say the client should do the
> following:
>
>    private final String serverURLin  = new String("http://localhost:9876/");
>    private URLConnection urlConClientIn;
>    private PrintStream clientOutStream;
>
>    <snip>
>
>    urlConClientOut = (new URL(serverURLin)).openConnection();
>    urlConClientOut.setDoOutput(true);
>    urlConClientOut.setUseCaches(false);
>    clientOutStream = new PrintStream( urlConClientOut.getOutputStream() );
>    clientOutStream.println("Linux");
>    clientOutStream.println("Is");
>    clientOutStream.println("Lovely");
>    clientOutStream.close();
>
> Yet this doesnt seem to send anything to the server. Now I know the server
> is running ok because by entering http://localhost:9876/ in netscape, it
> connects and the server gets all the header information from the url
> request.. so what's wrong with that client?
>
> Here is the server code I have:
>
>    private Socket socketIn;
>    private DataInputStream sInStream;
>
>    <snip stuff to assign socketIn a Socket from ServerSocket.accept()>
>
>    sInStream = new DataInputStream
>    (new BufferedInputStream(socketIn.getInputStream()));
>
>    int contentLength = 0;
>    String inStr;
>
>    // Read the headers
>    do
>    {
>      inStr = sInStream.readLine();
>      System.out.println(inStr);
>      // if inStr has Content-length, read the
>      // length into a variable here
>      if (inStr.startsWith("Content-length:"))
>      {
>        String s =
>        inStr.substring(16, inStr.length());
>        System.out.println("Got clength "+s);
>        contentLength = Integer.parseInt(s.trim());
>      }
>    } while (!inStr.equals (""));
>
>    But it always gets 'stuck' at the sInStream.readLine() !
>
>    If anyone has any pointers, or working code which does roughly the same
> as this, please help!
>
> Ta,
>
> John Baker
>
> --
> John Baker, Software Engineer, Java coder, Salad sarnie lover.
>  Work: (01203) 562000 ext 4153
>  Home: (01203) 601890


Reply via email to