Return-Path: Delivered-To: apmail-jakarta-tomcat-user-archive@apache.org Received: (qmail 80926 invoked from network); 13 Nov 2001 09:01:55 -0000 Received: from unknown (HELO nagoya.betaversion.org) (192.18.49.131) by daedalus.apache.org with SMTP; 13 Nov 2001 09:01:55 -0000 Received: (qmail 29040 invoked by uid 97); 13 Nov 2001 09:01:35 -0000 Delivered-To: qmlist-jakarta-archive-tomcat-user@jakarta.apache.org Received: (qmail 29023 invoked by uid 97); 13 Nov 2001 09:01:34 -0000 Mailing-List: contact tomcat-user-help@jakarta.apache.org; run by ezmlm Precedence: bulk List-Unsubscribe: List-Subscribe: List-Help: List-Post: List-Id: "Tomcat Users List" Reply-To: "Tomcat Users List" Delivered-To: mailing list tomcat-user@jakarta.apache.org Received: (qmail 29012 invoked from network); 13 Nov 2001 09:01:34 -0000 X-Authentication-Warning: jeeves.stud.ntnu.no: endrs owned process doing -bs Date: Tue, 13 Nov 2001 10:01:31 +0100 (MET) From: =?iso-8859-1?Q?Endre_St=F8lsvik?= X-X-Sender: To: Remy Maucherat , Tomcat Users List Subject: Re: [TC4] Sending redirect fails if OutputStream isn't closed .. In-Reply-To: <00e201c16bdc$9a45fe20$6a16a643@snvl1.sfba.home.com> Message-ID: X-My-Opinion: War and bombs are bad. Peace and flowers are good. ;-D MIME-Version: 1.0 Content-Type: TEXT/PLAIN; charset=US-ASCII X-Spam-Rating: daedalus.apache.org 1.6.2 0/1000/N X-Spam-Rating: daedalus.apache.org 1.6.2 0/1000/N On Mon, 12 Nov 2001, Remy Maucherat wrote: | > I'm using this program called "loadsim" to get a simulation of a bunch of | > users using my webapp. This is a java program utilizing the | > java.net.HttpConnection to simulate the clients, built using large parts | > of a apache loadsimulation system, right? Make that "java.net.HttpURLConnection" (as the client). | > Why is that? Why do I have to get the outputstream, and then just close it | > to get the sendRedirect away? Why isn't the stream closed on exit of the | > doGet/Post method? Or whatever? | | The default servlet doesn't use the output stream at all, and appears to | behave correctly (verified using a telnet session). This looks like a | problem with the HTTP client you're using. I also mentioned this in the first post (Mozilla behaves properly), but why does it then work when I get and close the output stream as described? Here's the code from "loadsim" that does the sampling, just if you don't have much other to do today! (It's not that long, actually).. It seems to close both its output to the server, and the input from the server.. Extra-info: that page which sends the redirects is just a "poster-page"; this is where the browser does GETs with parameters and POSTs: it just takes all the parameters from the browser, and then sends a "blank" redirect to the browser. This fools the (most) browsers to not store this as a separate history event, and you don't get that reposting stuff on "back" and "reload" going. -------------------------- /** * Send the link to the webserver using the VirtualUser's * CookieManager. * * @param cookieManager The VirtualUser's CookieManager. * @param log The log file to log information/errors to. * * @return The SampleResult object. * * @exception IOException if an error occurs reading from or writing to the * webserver. */ public SampleResult sample(CookieManager cookieManager, ILog log) throws IOException { long then; URL url = null; HttpURLConnection connection = null; BufferedInputStream input = null; SampleResult sample = null; byte [] buf = null; int bytesread = 0; ByteArrayOutputStream outbuf = null; // Init the sample result sample = new SampleResult(); sample.setLabel(id_); // Create the HTTP connection then = System.currentTimeMillis(); url = new URL(linkStr_); connection = (HttpURLConnection) url.openConnection(); connection.setUseCaches(false); connection.setDoInput(true); connection.setInstanceFollowRedirects(false); // *** frode@coretrek.no: if followredirects is true we miss out on the cookies set by the first page! // Setup cookies if (cookieManager != null) { String cookieHeader = cookieManager.getCookieHeaderForURL(url); if (cookieHeader != null) { connection.setRequestProperty("Cookie", cookieHeader); } } if (formDataStr_ == null) { connection.connect(); sample.setTTOpen(System.currentTimeMillis() - then); log.log(ILog.INFORMATION, "' url = " + linkStr_); } else { try { DataOutputStream output = null; connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); connection.setDoOutput(true); connection.setRequestMethod("POST"); connection.connect(); sample.setTTOpen(System.currentTimeMillis() - then); output = new DataOutputStream(connection.getOutputStream()); output.writeBytes(formDataStr_); output.flush(); output.close(); log.log(ILog.INFORMATION, "' url = " + linkStr_ + " POST = " + formDataStr_ + " (" + formDataStr_.length() + ")"); } catch (Exception e) { log.log(ILog.ERROR, "Caught exception: " + e.getMessage() + " ignoring sample"); sample.setTTLB(-1); sample.setTTFB(-1); sample.setSize(-1); sample.setLastPage(null); return sample; } } // this is where 'then = currentTime' used to be. sample.setTTReq(System.currentTimeMillis() - then); // Check for cookies coming back try { if (cookieManager != null) { // check for Set-Cookie headers from server (index 0 is the // HTTP repsonse code), which does not use "header: value" format for (int i = 1;; i++) { try { if (connection.getHeaderFieldKey(i).equals("Set-Cookie")) { cookieManager.addCookieFromHeader(connection.getHeaderField(i), url); } } catch (NullPointerException e) { break; } } } // Read the HTML from the webserver input = new BufferedInputStream(connection.getInputStream()); buf = new byte[BUFF_SIZE]; bytesread = input.read(buf); sample.setTTFB(System.currentTimeMillis() - then); outbuf = new ByteArrayOutputStream(); while (bytesread != -1) { outbuf.write(buf, 0, bytesread); bytesread = input.read(buf); } input.close(); sample.setTTLB(System.currentTimeMillis() - then); sample.setSize(outbuf.size()); if (connection.getContentType() != null) { sample.setContentType(connection.getContentType()); if (connection.getContentType().toLowerCase().startsWith("text/html")) { sample.setLastPage(outbuf.toString()); } else { sample.setLastPage(null); } } } catch (Exception e) { log.log(ILog.ERROR, "Caught exception: " + e.getMessage() + " ignoring sample"); sample.setTTLB(-1); sample.setTTFB(-1); sample.setSize(-1); sample.setLastPage(null); return sample; } finally { if (input != null) { input.close(); } } if (sample.getLastPage() != null && expectTitle_ != null) { String receivedTitle = getTitle(buf.toString()); if (!expectTitle_.trim().equals(receivedTitle.trim())) { log.log(ILog.ERROR, "' Expected title: '" + expectTitle_ + "' got '" + receivedTitle + "'"); } } return sample; } -------------------------- -- Mvh, Endre -- To unsubscribe: For additional commands: Troubles with the list: