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 Zhenyu Wang <zh...@rentmarketer.com> on 2006/12/13 08:30:00 UTC

Problem with MultiPart Post - Uploading image

Hello All,

I have the problem with uploading images using the PostMethod of commons
HTTPClient (3.0.1).  The method is used to upload a .jpg image to one
external site along with posting other form text fields.   However, I got
the error message returned from the server when I excute the following code
- "Files not uploaded.  We only accept .jpg, or gif".  Strangely, the same
code has been  used  to upload the images to other external sites
sucessfully.  Any suggestion why is this happening?  Thanks.

// HTML form
<form name="newlist" action="
http://www.domainname.com/index.cfm?fuseaction=newlist"
enctype="multipart/form-data" method="POST" enablecab="NO">

  <input type="text name="text-field">
  <input type="file" name="photofile" value="" accept="image/gif,image/jpg">
  <input type="image" src="/upload.gif" alt="upload image" onClick="
document.newlist.submit();">

</form>

// posting code
public static void main(String[] args)
{
    ....

    PostMethod lPostMethod = new
PostMethod("http://www.domainname.com/index.cfm?fuseaction=newlist");
    List lParts = new ArrayList();

    // add string part
    lParts.add(new StringPart("text-field", "abc"));

    // add file part
    try
    {
        File lImageFile = new File("C:\\heart.jpg");
        FilePart lFilePart = new FilePart("photofile", lFile);
        lFilePart.setContentType("image/jpeg");
        lParts.add(lFilePart);

        MultipartRequestEntity multipartRequestEntity =
            new MultipartRequestEntity(lParts.toArray(new Part[0]),
lPostMethod.getParams());
        lPostMethod.setRequestEntity(multipartRequestEntity);

        HttpClient client = new HttpClient();

        int status = client.executeMethod(lPostMethod);
        if (status == 200) {
            String response = post.getResponseBodyAsString();
            System.out.println(attachmentGuid);
        }
    } catch (IOException e) {
      e.printStackTrace();
    } finally {
      post.releaseConnection();
    }
}

Re: Problem with MultiPart Post - Uploading image

Posted by Roland Weber <ht...@dubioso.net>.
Hello Marshall,

I see you've fixed the problem while I was typing this mail.
but since I've typed it already, I'm gonna send it anyway... :-)


> I can catch what the browser and httpclient send through TCPMon tool (see
> the below).  You can see there are quite different in both header and body
> sections.  Question is what I should do to make my code to send exactly the
> same as the browser does?

not all of the differences are significant.
Here are a few suggestions:

1. enforce sending of a single Cookie header:
http://jakarta.apache.org/commons/httpclient/apidocs/org/apache/commons/httpclient/params/HttpMethodParams.html#SINGLE_COOKIE_HEADER

2. set the User-Agent header to a standard browser value:
http://jakarta.apache.org/commons/httpclient/apidocs/org/apache/commons/httpclient/HttpMethod.html#setRequestHeader(java.lang.String,%20java.lang.String)

3. add the Referer header (same method as for 2)

4. add the Accept-Charset header (same method as for 2)


If none of these help, search the mailing list archive for previous
discussions of the MultipartRequestEntity. The differences in the
body _should_not_ be significant, but you never know what a server
makes out of it. I'm a bit surprised about the charset= in the
image/jpeg part, but that's only an oddity and not really a problem.

(Edit: here I was obviously wrong :-)

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


Re: Problem with MultiPart Post - Uploading image

Posted by Julius Davies <ju...@gmail.com>.
To me, this looks suspicious:

Content-Type: image/jpeg; charset=UTF-8


You probably need to set the Content-Type to be just "image/jpeg".  That
charset you're adding doesn't make sense.

yours,

Julius


On 12/14/06, Zhenyu Wang <zh...@rentmarketer.com> wrote:
>
> Hello Roland,
>
> I can catch what the browser and httpclient send through TCPMon tool (see
> the below).  You can see there are quite different in both header and body
> sections.  Question is what I should do to make my code to send exactly
> the
> same as the browser does?
>
> HTTPClient sends:
>
> ---------------------------------------------------------------------------------------------------------------------------------------
> POST /index.cfm?fuseaction=lmanage.editListingPhoto HTTP/1.1
> User-Agent: Jakarta Commons-HttpClient/3.0.1
> Host: www.apartmentfrog.com
> Cookie: $Version=0; CFID=1108839; $Path=/
> Cookie: $Version=0;
> CFTOKEN=d1b690cd1acf9a79-8213869B-C51B-A0AA-470EC106CB9422E6; $Path=/
> Cookie: $Version=0; JSESSIONID=c8307c79598c623d7b4e; $Path=/
> Content-Length: 11254
> Content-Type: multipart/form-data;
> boundary=Q8Q7z887xNVDp38wYwwEPreXSXKFNuPihInCt
>
> --Q8Q7z887xNVDp38wYwwEPreXSXKFNuPihInCt
> Content-Disposition: form-data; name="AFID"
> Content-Type: text/plain; charset=US-ASCII
> Content-Transfer-Encoding: 8bit
>
> 3775
> --Q8Q7z887xNVDp38wYwwEPreXSXKFNuPihInCt
> Content-Disposition: form-data; name="useraction"
> Content-Type: text/plain; charset=US-ASCII
> Content-Transfer-Encoding: 8bit
>
> uploadphoto
> --Q8Q7z887xNVDp38wYwwEPreXSXKFNuPihInCt
> Content-Disposition: form-data; name="photocaption"
> Content-Type: text/plain; charset=US-ASCII
> Content-Transfer-Encoding: 8bit
>
>
> --Q8Q7z887xNVDp38wYwwEPreXSXKFNuPihInCt
> Content-Disposition: form-data; name="photofile"; filename="love.jpg"
> Content-Type: image/jpeg; charset=UTF-8
> Content-Transfer-Encoding: binary
> ........ binary code
> --Q8Q7z887xNVDp38wYwwEPreXSXKFNuPihInCt--
>
> Browser Send:
>
> -----------------------------------------------------------------------------------------------------------------------------
> POST /index.cfm?fuseaction=lmanage.editListingPhoto HTTP/1.1
> Host: www.apartmentfrog.com
> User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.0.8)
> Gecko/20061025 Firefox/1.5.0.8
> Accept: text/xml,application/xml,application/xhtml+xml,text/html;q=0.9
> ,text/plain;q=0.8,image/png,*/*;q=0.5
> Accept-Language: en-us,en;q=0.5
> Accept-Encoding: gzip,deflate
> Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7
> Keep-Alive: 300
> Connection: keep-alive
> Referer:
>
> http://www.apartmentfrog.com/index.cfm?fuseaction=lmanage.editListingPhotoForm&AFID=3766
> Cookie: CFID=1106548;
> CFTOKEN=905a3588a60294cb-7E7DDD76-C3A5-8795-939DB218C66689EC;
> JSESSIONID=c830101025f739275d7f
> Content-Type: multipart/form-data;
> boundary=---------------------------23281168279961
> Content-Length: 10993
>
> -----------------------------23281168279961
> Content-Disposition: form-data; name="AFID"
>
> 3766
> -----------------------------23281168279961
> Content-Disposition: form-data; name="useraction"
>
> uploadphoto
> -----------------------------23281168279961
> Content-Disposition: form-data; name="photocaption"
>
>
> -----------------------------23281168279961
> Content-Disposition: form-data; name="photofile"; filename="love.jpg"
> Content-Type: image/jpeg
>
> ........................ binary code
>
>
> Thanks,
> Marshall
>
>
> On 12/13/06, Roland Weber <RO...@de.ibm.com> wrote:
> >
> > Hello Marshall,
> >
> > > It's a really good documentation!
> >
> > Thanks :-)
> >
> > > I used a tool called TCPMON to catch the
> > > request header, and the following is what I got:
> > >
> > > POST /index.cfm?fuseaction=landlords.ListingValidate HTTP/1.1
> > > User-Agent: Jakarta Commons-HttpClient/3.0.1
> >
> > You can see what HttpClient is sending by enabling the wire log:
> > http://jakarta.apache.org/commons/httpclient/logging.html
> >
> > The question is: what does the _browser_ send, and what
> > are the differences to what HttpClient is sending?
> >
> > cheers,
> >   Roland
> >
> >
> >
> >
> >
>
>


-- 
yours,

Julius Davies
416-652-0183
http://juliusdavies.ca/

Re: Problem with MultiPart Post - Uploading image

Posted by Zhenyu Wang <zh...@rentmarketer.com>.
Hello Roland,

I can catch what the browser and httpclient send through TCPMon tool (see
the below).  You can see there are quite different in both header and body
sections.  Question is what I should do to make my code to send exactly the
same as the browser does?

HTTPClient sends:
---------------------------------------------------------------------------------------------------------------------------------------
POST /index.cfm?fuseaction=lmanage.editListingPhoto HTTP/1.1
User-Agent: Jakarta Commons-HttpClient/3.0.1
Host: www.apartmentfrog.com
Cookie: $Version=0; CFID=1108839; $Path=/
Cookie: $Version=0;
CFTOKEN=d1b690cd1acf9a79-8213869B-C51B-A0AA-470EC106CB9422E6; $Path=/
Cookie: $Version=0; JSESSIONID=c8307c79598c623d7b4e; $Path=/
Content-Length: 11254
Content-Type: multipart/form-data;
boundary=Q8Q7z887xNVDp38wYwwEPreXSXKFNuPihInCt

--Q8Q7z887xNVDp38wYwwEPreXSXKFNuPihInCt
Content-Disposition: form-data; name="AFID"
Content-Type: text/plain; charset=US-ASCII
Content-Transfer-Encoding: 8bit

3775
--Q8Q7z887xNVDp38wYwwEPreXSXKFNuPihInCt
Content-Disposition: form-data; name="useraction"
Content-Type: text/plain; charset=US-ASCII
Content-Transfer-Encoding: 8bit

uploadphoto
--Q8Q7z887xNVDp38wYwwEPreXSXKFNuPihInCt
Content-Disposition: form-data; name="photocaption"
Content-Type: text/plain; charset=US-ASCII
Content-Transfer-Encoding: 8bit


--Q8Q7z887xNVDp38wYwwEPreXSXKFNuPihInCt
Content-Disposition: form-data; name="photofile"; filename="love.jpg"
Content-Type: image/jpeg; charset=UTF-8
Content-Transfer-Encoding: binary
 ........ binary code
--Q8Q7z887xNVDp38wYwwEPreXSXKFNuPihInCt--

Browser Send:
-----------------------------------------------------------------------------------------------------------------------------
POST /index.cfm?fuseaction=lmanage.editListingPhoto HTTP/1.1
Host: www.apartmentfrog.com
User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.0.8)
Gecko/20061025 Firefox/1.5.0.8
Accept: text/xml,application/xml,application/xhtml+xml,text/html;q=0.9
,text/plain;q=0.8,image/png,*/*;q=0.5
Accept-Language: en-us,en;q=0.5
Accept-Encoding: gzip,deflate
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7
Keep-Alive: 300
Connection: keep-alive
Referer:
http://www.apartmentfrog.com/index.cfm?fuseaction=lmanage.editListingPhotoForm&AFID=3766
Cookie: CFID=1106548;
CFTOKEN=905a3588a60294cb-7E7DDD76-C3A5-8795-939DB218C66689EC;
JSESSIONID=c830101025f739275d7f
Content-Type: multipart/form-data;
boundary=---------------------------23281168279961
Content-Length: 10993

-----------------------------23281168279961
Content-Disposition: form-data; name="AFID"

3766
-----------------------------23281168279961
Content-Disposition: form-data; name="useraction"

uploadphoto
-----------------------------23281168279961
Content-Disposition: form-data; name="photocaption"


-----------------------------23281168279961
Content-Disposition: form-data; name="photofile"; filename="love.jpg"
Content-Type: image/jpeg

........................ binary code


Thanks,
Marshall


On 12/13/06, Roland Weber <RO...@de.ibm.com> wrote:
>
> Hello Marshall,
>
> > It's a really good documentation!
>
> Thanks :-)
>
> > I used a tool called TCPMON to catch the
> > request header, and the following is what I got:
> >
> > POST /index.cfm?fuseaction=landlords.ListingValidate HTTP/1.1
> > User-Agent: Jakarta Commons-HttpClient/3.0.1
>
> You can see what HttpClient is sending by enabling the wire log:
> http://jakarta.apache.org/commons/httpclient/logging.html
>
> The question is: what does the _browser_ send, and what
> are the differences to what HttpClient is sending?
>
> cheers,
>   Roland
>
>
>
>
>

Re: Problem with MultiPart Post - Uploading image

Posted by Zhenyu Wang <zh...@rentmarketer.com>.
Hello Roland,

I got it!!!  It's caused by charset.  What I did is to set the charset of
filepart to null.

Thank you very much for your help!

Cheers!
Marshall

On 12/13/06, Roland Weber <RO...@de.ibm.com> wrote:
>
> Hello Marshall,
>
> > It's a really good documentation!
>
> Thanks :-)
>
> > I used a tool called TCPMON to catch the
> > request header, and the following is what I got:
> >
> > POST /index.cfm?fuseaction=landlords.ListingValidate HTTP/1.1
> > User-Agent: Jakarta Commons-HttpClient/3.0.1
>
> You can see what HttpClient is sending by enabling the wire log:
> http://jakarta.apache.org/commons/httpclient/logging.html
>
> The question is: what does the _browser_ send, and what
> are the differences to what HttpClient is sending?
>
> cheers,
>   Roland
>
>
>
>
>

Re: Problem with MultiPart Post - Uploading image

Posted by Roland Weber <RO...@de.ibm.com>.
Hello Marshall,

> It's a really good documentation!

Thanks :-)

> I used a tool called TCPMON to catch the
> request header, and the following is what I got:
> 
> POST /index.cfm?fuseaction=landlords.ListingValidate HTTP/1.1
> User-Agent: Jakarta Commons-HttpClient/3.0.1

You can see what HttpClient is sending by enabling the wire log:
http://jakarta.apache.org/commons/httpclient/logging.html

The question is: what does the _browser_ send, and what
are the differences to what HttpClient is sending?

cheers,
  Roland



Re: Problem with MultiPart Post - Uploading image

Posted by Zhenyu Wang <zh...@rentmarketer.com>.
Hello Roland,

The file is the JPEG file.  I can manually upload that image from the site (
www.apartmentfrog.com).

It's a really good documentation! I used a tool called TCPMON to catch the
request header, and the following is what I got:

POST /index.cfm?fuseaction=landlords.ListingValidate HTTP/1.1
User-Agent: Jakarta Commons-HttpClient/3.0.1
Host: localhost
Cookie: $Version=0; CFID=1106460; $Path=/
Cookie: $Version=0;
CFTOKEN=37b1d13db38db2f-7E3DE0AA-E33F-E5EF-4CC1D84DB2A7521D; $Path=/
Cookie: $Version=0; JSESSIONID=c830b371add8311a6e31; $Path=/
Content-Length: 11262
Content-Type: multipart/form-data;
boundary=oAGaxHAa4P7Wm82FQVRHbgpK-xPv4qSeW3jt9k26

--oAGaxHAa4P7Wm82FQVRHbgpK-xPv4qSeW3jt9k26
Content-Disposition: form-data; name="photocaption"
Content-Type: text/plain; charset=US-ASCII
Content-Transfer-Encoding: 8bit


--oAGaxHAa4P7Wm82FQVRHbgpK-xPv4qSeW3jt9k26
Content-Disposition: form-data; name="photofile"; filename="love.jpg"
Content-Type: image/jpeg; charset=ISO-8859-1
Content-Transfer-Encoding: binary

........... some binary code here

--oAGaxHAa4P7Wm82FQVRHbgpK-xPv4qSeW3jt9k26
Content-Disposition: form-data; name="PHOTODATA"
Content-Type: text/plain; charset=US-ASCII
Content-Transfer-Encoding: 8bit


--oAGaxHAa4P7Wm82FQVRHbgpK-xPv4qSeW3jt9k26
Content-Disposition: form-data; name="TEMPAFID"
Content-Type: text/plain; charset=US-ASCII
Content-Transfer-Encoding: 8bit


--oAGaxHAa4P7Wm82FQVRHbgpK-xPv4qSeW3jt9k26--

Thanks,
Marshall

On 12/13/06, Roland Weber <ht...@dubioso.net> wrote:
>
> Hello Marshall,
>
> > I thought the name for the file part should be match with the name of
> file
> > input type in the HTML form.
>
> Hmm, it probably should. I assume the file you are uploading
> is actually a JPEG file. Please try the instructions in our
> Client HTTP Programming Primer for analyzing HTML forms and,
> if necessary, sniffing the request generated by a browser:
>
> http://wiki.apache.org/jakarta-httpclient/ForAbsoluteBeginners
>
> 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
>
>

Re: Problem with MultiPart Post - Uploading image

Posted by Roland Weber <ht...@dubioso.net>.
Hello Marshall,

> I thought the name for the file part should be match with the name of file
> input type in the HTML form.

Hmm, it probably should. I assume the file you are uploading
is actually a JPEG file. Please try the instructions in our
Client HTTP Programming Primer for analyzing HTML forms and,
if necessary, sniffing the request generated by a browser:

http://wiki.apache.org/jakarta-httpclient/ForAbsoluteBeginners

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


Re: Problem with MultiPart Post - Uploading image

Posted by Zhenyu Wang <zh...@rentmarketer.com>.
Hello,

I thought the name for the file part should be match with the name of file
input type in the HTML form.  However, I did change the code as your
suggestion, it didn't work.

FilePart lFilePart = new FilePart("photofile.jpg", lFile)

Thanks,
Marshall


On 12/13/06, Roland Weber <RO...@de.ibm.com> wrote:
>
> Hello,
>
> "Zhenyu Wang" <zh...@rentmarketer.com> wrote on 13.12.2006 08:30:00:
>
> > - "Files not uploaded.  We only accept .jpg, or gif".  Strangely, the
> same
> > code has been  used  to upload the images to other external sites
> > sucessfully.  Any suggestion why is this happening?  Thanks.
>
> I guess other external sites don't check the file type, or don't
> check it in the same way as this site.
>
> >         File lImageFile = new File("C:\\heart.jpg");
> >         FilePart lFilePart = new FilePart("photofile", lFile);
> >         lFilePart.setContentType("image/jpeg");
>
> Have you tried "photofile.jpg" as the file part name?
>
> hope that helps,
>   Roland
>
>
>
>

Re: Problem with MultiPart Post - Uploading image

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

"Zhenyu Wang" <zh...@rentmarketer.com> wrote on 13.12.2006 08:30:00:

> - "Files not uploaded.  We only accept .jpg, or gif".  Strangely, the 
same
> code has been  used  to upload the images to other external sites
> sucessfully.  Any suggestion why is this happening?  Thanks.

I guess other external sites don't check the file type, or don't
check it in the same way as this site.

>         File lImageFile = new File("C:\\heart.jpg");
>         FilePart lFilePart = new FilePart("photofile", lFile);
>         lFilePart.setContentType("image/jpeg");

Have you tried "photofile.jpg" as the file part name?

hope that helps,
  Roland