You are viewing a plain text version of this content. The canonical link for it is here.
Posted to httpclient-users@hc.apache.org by Trudy Little <tr...@netdirector.biz> on 2005/08/16 20:49:19 UTC

Having trouble with MultipartRequestEntity

Hi,

I am attempting to use the MultipartRequestEntity to upload a file and
some additional parameters via HTTP to a client.  The process works fine
if I am sending to another Java client, but I am getting some additional
text on all the string parameters when sending to non-Java clients (ASP,
Perl etc).  I am getting "Content-Transfer-Encoding: 8bit" appended to
the beginning of each parameter value.  I have confirmed that this does
not happen if I submit the file using a standard HTTP page.  The problem
also does not occur if I am just using the PostMethod without sending
file data.  I am using the latest release of HTTPClient.

Sample Code:

public String postHttpFile(String pTargetUrl, String pFileName,
                            String pUserName, String pPassword) throws
Exception {

    String vErrorText = "";
    PostMethod filePost = null;
    java.io.File vFile = null;
    Exception vException = null;
    String vResponse = "";

    try {
      vErrorText = "Error retrieving file: " + pFileName;
      vFile = new File(pFileName);


      filePost = new PostMethod(pTargetUrl);
      Part[] parts = { new StringPart("userid", pUserName),
           new StringPart("password", pPassword),
           new StringPart("filesize", Long.toString(vFile.length())),
           new FilePart(vFile.getName(), vFile) };

      filePost.setRequestEntity( new MultipartRequestEntity(parts,
filePost.getParams()) );

      HttpClient client = new HttpClient();


      //Post file.
      vErrorText = "Error executing post method";
      int status = client.executeMethod(filePost);

      if (status != HttpStatus.SC_OK) {
        vErrorText = "Error posting file. Post Response: " +
HttpStatus.getStatusText(status);
        throw new Exception(vErrorText);
      }

      vResponse = filePost.getResponseBodyAsString();

    } catch (Exception ex) {
      vException = ex;
    } finally {

      //Release file handle
      if(filePost != null) {
        filePost.releaseConnection();
      }

      //Throw any generated exception back up.
      if(vException != null) {
        throw new Exception(vErrorText, vException);
      }
    }

    return vResponse;

  }

If UserId = abcdefg, non-Java clients are getting:
"Content-Transfer-Encoding: 8bit

abcdefg" 




Trudy Little
Principal Software Engineer - NetDirector
Florida Default Law Group, PL
(813) 342-2200 ext 3142

DISCLAIMER : The information contained in this mail is attorney
privileged and confidential. It is intended only for the use of the
individual or entity named above. If the reader of this message is not
the intended recipient you are hereby notified that any dissemination
distribution or copy of this communication is strictly prohibited. If
you have received this communication in error please notify us
immediately by replying to this email address and then deleting it from
your database



Re: Having trouble with MultipartRequestEntity

Posted by Roland Weber <RO...@de.ibm.com>.
Hi Trudy,

> I am attempting to use the MultipartRequestEntity to upload a file and
> some additional parameters via HTTP to a client.  The process works fine

How do you upload to a *client*? HttpClient is the client, the other side
of the communication needs to be an HTTP server. HTTP is not a peer-2-peer
protocol where clients can communicate with eachother.

> if I am sending to another Java client, but I am getting some additional
> text on all the string parameters when sending to non-Java clients (ASP,
> Perl etc).  I am getting "Content-Transfer-Encoding: 8bit" appended to
> the beginning of each parameter value.  I have confirmed that this does

That part header is specified by RFC 1521. I assume that your ASP, Perl
and other environments just don't implement that standard (correctly),
while the Java environment does.

> not happen if I submit the file using a standard HTTP page.  The problem
> also does not occur if I am just using the PostMethod without sending
> file data.  I am using the latest release of HTTPClient.

That's strange, since the StringPart use Content-Transfer-Encoding: 8bit,
while the file parts use C-T-E: binary by default. Maybe your non-Java
environments can't handle different C-T-Es in a single request?

Try PartBase.setTransferEncoding(null) on the string and file parts. This
should prevent the C-T-E headers from being sent.

hope that helps,
  Roland


---------------------------------------------------------------------
To unsubscribe, e-mail: httpclient-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: httpclient-user-help@jakarta.apache.org