hc-httpclient-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Roland Weber <ROLWE...@de.ibm.com>
Subject Re: Setting request parameters
Date Fri, 25 Feb 2005 07:41:09 GMT
Hello Per,

Version 1 works as you mention. Version 3 has no chance at all,
since client parameters are not request parameters. In version 2,
there is a programming error:

    httppost.addParameter("TYPE", "upload")
    httppost.addParameter("FILE_NAME", file.getName()) 
    httppost.setRequestEntity(...)

The two calls to addParameter() tell the PostMethod to create
a multipart request body with the specified parameters. The
third call to setRequestEntity() then discards that body and
replaces it with the file contents.
To send a multipart request with parameters and a file, you
have to use the MultipartPostMethod and set three parts, two
StringParts for the parameters and a FilePart for the file
to upload. The method will then combine all three parts into
a multipart request body.

hope that helps,
  Roland





Per Nyfelt <per.nyfelt@resourcing.se> 
24.02.2005 17:48
Please respond to
"HttpClient User Discussion"


To
httpclient-user@jakarta.apache.org
cc

Subject
Setting request parameters






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



Mime
  • Unnamed multipart/alternative (inline, None, 0 bytes)
View raw message