Return-Path: Delivered-To: apmail-jakarta-httpclient-user-archive@www.apache.org Received: (qmail 82212 invoked from network); 24 Feb 2005 17:21:52 -0000 Received: from hermes.apache.org (HELO mail.apache.org) (209.237.227.199) by minotaur-2.apache.org with SMTP; 24 Feb 2005 17:21:52 -0000 Received: (qmail 63294 invoked by uid 500); 24 Feb 2005 17:21:52 -0000 Delivered-To: apmail-jakarta-httpclient-user-archive@jakarta.apache.org Received: (qmail 63275 invoked by uid 500); 24 Feb 2005 17:21:52 -0000 Mailing-List: contact httpclient-user-help@jakarta.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: "HttpClient User Discussion" Reply-To: "HttpClient User Discussion" Delivered-To: mailing list httpclient-user@jakarta.apache.org Delivered-To: moderator for httpclient-user@jakarta.apache.org Received: (qmail 91208 invoked by uid 99); 24 Feb 2005 16:48:34 -0000 X-ASF-Spam-Status: No, hits=0.0 required=10.0 tests= X-Spam-Check-By: apache.org Received-SPF: neutral (hermes.apache.org: local policy) From: Per Nyfelt Organization: Resourcing Networks To: httpclient-user@jakarta.apache.org Subject: Setting request parameters Date: Thu, 24 Feb 2005 17:48:12 +0100 User-Agent: KMail/1.7.2 MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Content-Disposition: inline Message-Id: <200502241748.12796.per.nyfelt@resourcing.se> X-Virus-Checked: Checked X-Spam-Rating: minotaur-2.apache.org 1.6.2 0/1000/N I have a servlet running in Tomcat 5 that i want to handle file transfers (upload and download) of big files (>32 MB). However, I am unable to find a way for HttpClient to set parameters properly for the servlet to read them. The servlet has a service() method defined with the following logic: public void service(HttpServletRequest request, HttpServletResponse response) { String requestType = request.getParameter("TYPE"); String fileName= request.getParameter("FILE_NAME"); if ("upload".equals(requestType)) { uploadDocument(fileName, request, response); } else if ("download".equals(requestType)) { downloadDocument(fileName, response); } else { sendParameterError(requestType, fileName); } } The issue is that requestType and fileName end up as null for all my attempts to set parameters. Here's some examples of code I've tried for the upload part (I am using 3.0 rc1): 1. This is the only one that works (i.e. putting the request parameters in the url): public void testUpload() { File file = new File("test.zip"); HttpClient client = new HttpClient(); PostMethod httppost = new PostMethod("http://localhost:8080/test/FiletServlet?TYPE=download&FILE_NAME=" + file.getName()); httppost.setRequestEntity(new InputStreamRequestEntity( new FileInputStream(file), file.length())); try { client.executeMethod(httppost); if (httppost.getStatusCode() == HttpStatus.SC_OK) { System.out.println(httppost.getResponseBodyAsString()); } else { System.out.println("Unexpected failure: " + httppost.getStatusLine().toString()); } } finally { httppost.releaseConnection(); } } 2. This is what I would like to do: public void testUpload2() { File file = new File("test.zip"); HttpClient client = new HttpClient(); PostMethod httppost = new PostMethod("http://localhost:8080/test/FileServlet"); httppost.addParameter("TYPE", "upload"); httppost.addParameter("FILE_NAME", file.getName()); httppost.setRequestEntity(new InputStreamRequestEntity( new FileInputStream(file), file.length())); try { client.executeMethod(httppost); if (httppost.getStatusCode() == HttpStatus.SC_OK) { System.out.println(httppost.getResponseBodyAsString()); } else { System.out.println("Unexpected failure: " + httppost.getStatusLine().toString()); } } finally { httppost.releaseConnection(); } } 3. I have also tried setting parameters in the HttpClient: File file = new File("test.zip"); HttpClient client = new HttpClient(); client.getParams().setParameter("TYPE", "upload"); client.getParams().setParameter("FILE_NAME", file.getName()); PostMethod httppost = new PostMethod("http://localhost:8080/test/FileServlet"); httppost.setRequestEntity(new InputStreamRequestEntity( new FileInputStream(file), file.length())); try { client.executeMethod(httppost); ....etc... 4. and also tried using NameValuePair: File file = new File("test.zip"); HttpClient client = new HttpClient(); PostMethod httppost = new PostMethod("http://localhost:8080/test/FileServlet"); NameValuePair type = new NameValuePair("TYPE", "upload"); NameValuePair fileName = new NameValuePair("FILE_NAME", file.getName()); httppost.setRequestBody(new NameValuePair[] {type, fileName}); httppost.setRequestEntity(new InputStreamRequestEntity( new FileInputStream(file), file.length())); try { client.executeMethod(httppost); ...etc..... But all variations on this theme given me null on both parameters on the servlet side. Maybe I have misunderstood something but is there no way to set request parameters one by one and sending them in the POST instead of building up a GET-type string with all parameters passed in the constructor for the PostMethod? Please advice. Best regards, Per --------------------------------------------------------------------- To unsubscribe, e-mail: httpclient-user-unsubscribe@jakarta.apache.org For additional commands, e-mail: httpclient-user-help@jakarta.apache.org