You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tomcat.apache.org by Pankaj Tiwari <pa...@gmail.com> on 2011/01/03 04:06:55 UTC

multipart/form-data support

I have been trying to send multipart/form-data to the server. I have found
that the request never reaches the server.
This is my client code, if I am missing something core in here.

    public static void main(String args[])
    {
        try
        {
            URL url = new URL(SERVERURL);
            HttpURLConnection con = (HttpURLConnection)
url.openConnection();

            con.setDoInput(true);
            con.setDoOutput(true);
            con.setUseCaches(false);

            con.setRequestProperty("Content-Type",
"multipart/form-data;boundary=" + BOUNDARY);
            con.setRequestMethod("POST");
            con.setRequestProperty("Connection", "Keep-Alive");

            OutputStream out = con.getOutputStream();
            DataOutputStream oos = new DataOutputStream(out);
            oos.writeBytes(TWOHYPHENS + BOUNDARY + LINEEND);
            oos.writeBytes("Content-Disposition: form-data; name=\"name\"" +
LINEEND);
            oos.writeBytes(LINEEND);
            oos.write("HI".getBytes("UTF-8"));
            oos.writeBytes(LINEEND);
            oos.writeBytes(TWOHYPHENS + BOUNDARY + TWOHYPHENS + LINEEND);
            oos.flush();
            con.connect();
            oos.close();
        }
        catch (MalformedURLException e)
        {
            e.printStackTrace();
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }
    }

Thanks,
Pankaj

Re: multipart/form-data support

Posted by Pankaj Tiwari <pa...@gmail.com>.
Hi Chris,

Thank you so much for pointing me to the right direction. The problem was
with my SERVERURL. The server was sending back the response as 200.

I should have checked that before :(

Thanks Again,
Pankaj

On Mon, Jan 3, 2011 at 11:29 PM, Christopher Schultz <
chris@christopherschultz.net> wrote:

> -----BEGIN PGP SIGNED MESSAGE-----
> Hash: SHA1
>
> Pankaj,
>
> On 1/2/2011 10:06 PM, Pankaj Tiwari wrote:
> > I have been trying to send multipart/form-data to the server. I have
> found
> > that the request never reaches the server.
>
> How are you checking to see if it reaches the server?
>
> > This is my client code, if I am missing something core in here.
> >
> >     public static void main(String args[])
> >     {
> >         try
> >         {
> >             URL url = new URL(SERVERURL);
>
> What is SERVERURL?
>
> >             con.setDoInput(true);
> >             con.setDoOutput(true);
>
> ...
>
> >             OutputStream out = con.getOutputStream();
> >             DataOutputStream oos = new DataOutputStream(out);
>
> I agree with Pid: why are you using DataOutputStream?
>
> >             oos.writeBytes(TWOHYPHENS + BOUNDARY + LINEEND);
>
> Hmm... write before connect?
>
> >             oos.flush();
> >             con.connect();
>
> Try connecting before writing. Also, try reading the response (you did
> setDoInput(true)...).
>
> - -chris
> -----BEGIN PGP SIGNATURE-----
> Version: GnuPG v1.4.10 (MingW32)
> Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/
>
> iEYEARECAAYFAk0iDnEACgkQ9CaO5/Lv0PD6AQCfctFc2yULc7QkATMx1fAqwe5F
> km4AniigJu80/uJ6gl1Kg/6WD1i+rv/I
> =KmrY
> -----END PGP SIGNATURE-----
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
> For additional commands, e-mail: users-help@tomcat.apache.org
>
>


-- 
Pankaj Tiwari

Re: multipart/form-data support

Posted by Christopher Schultz <ch...@christopherschultz.net>.
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Pankaj,

On 1/2/2011 10:06 PM, Pankaj Tiwari wrote:
> I have been trying to send multipart/form-data to the server. I have found
> that the request never reaches the server.

How are you checking to see if it reaches the server?

> This is my client code, if I am missing something core in here.
> 
>     public static void main(String args[])
>     {
>         try
>         {
>             URL url = new URL(SERVERURL);

What is SERVERURL?

>             con.setDoInput(true);
>             con.setDoOutput(true);

...

>             OutputStream out = con.getOutputStream();
>             DataOutputStream oos = new DataOutputStream(out);

I agree with Pid: why are you using DataOutputStream?

>             oos.writeBytes(TWOHYPHENS + BOUNDARY + LINEEND);

Hmm... write before connect?

>             oos.flush();
>             con.connect();

Try connecting before writing. Also, try reading the response (you did
setDoInput(true)...).

- -chris
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.10 (MingW32)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/

iEYEARECAAYFAk0iDnEACgkQ9CaO5/Lv0PD6AQCfctFc2yULc7QkATMx1fAqwe5F
km4AniigJu80/uJ6gl1Kg/6WD1i+rv/I
=KmrY
-----END PGP SIGNATURE-----

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org


[OT] Re: multipart/form-data support

Posted by Pid <pi...@pidster.com>.
On 1/3/11 2:55 PM, Pankaj Tiwari wrote:
> 1. Keep-Alive - I am not sure if that should cause any issues here.

Why set it if you're not reusing the connection?

> 2. The request never reaches the server. I have tried without the last
> connect as well but nothing seems to work here.

Check the response status code: con.getResponseCode()

> When I run this piece of code, I do not get any exceptions or error on the
> client side but the request does not seems to be reaching the server.
> (doGet/doPost methods are never called).
> 
> Q1. How can i debug if the request is being sent from the java client at
> all? If it had been a browser, I could have used a header monitoring plugin
> but not sure about java application

GIYF: Wireshark.

> Q2. Is there any chance that tomcat does not support or ignores such request
> (multipart file-data)?

Tomcat doesn't care what the body of the request looks like.


p

> On Mon, Jan 3, 2011 at 7:53 PM, Pid <pi...@pidster.com> wrote:
> 
>> On 1/3/11 3:06 AM, Pankaj Tiwari wrote:
>>> I have been trying to send multipart/form-data to the server. I have
>> found
>>> that the request never reaches the server.
>>
>> Doesn't sound like a Tomcat problem to me then...
>>
>>> This is my client code, if I am missing something core in here.
>>>
>>>     public static void main(String args[])
>>>     {
>>>         try
>>>         {
>>>             URL url = new URL(SERVERURL);
>>>             HttpURLConnection con = (HttpURLConnection)
>>> url.openConnection();
>>>
>>>             con.setDoInput(true);
>>>             con.setDoOutput(true);
>>>             con.setUseCaches(false);
>>>
>>>             con.setRequestProperty("Content-Type",
>>> "multipart/form-data;boundary=" + BOUNDARY);
>>>             con.setRequestMethod("POST");
>>
>> Why are you using keep alive?
>>
>>>             con.setRequestProperty("Connection", "Keep-Alive");
>>>
>>>             OutputStream out = con.getOutputStream();
>>
>> Why are you using a DataOutputStream?
>>
>>>             DataOutputStream oos = new DataOutputStream(out);
>>>             oos.writeBytes(TWOHYPHENS + BOUNDARY + LINEEND);
>>>             oos.writeBytes("Content-Disposition: form-data;
>> name=\"name\"" +
>>> LINEEND);
>>>             oos.writeBytes(LINEEND);
>>>             oos.write("HI".getBytes("UTF-8"));
>>>             oos.writeBytes(LINEEND);
>>>             oos.writeBytes(TWOHYPHENS + BOUNDARY + TWOHYPHENS + LINEEND);
>>>             oos.flush();
>>>             con.connect();
>>
>> Why are you connecting after you've attempted to write data?
>>
>>
>> p
>>
>>>             oos.close();
>>>         }
>>>         catch (MalformedURLException e)
>>>         {
>>>             e.printStackTrace();
>>>         }
>>>         catch (IOException e)
>>>         {
>>>             e.printStackTrace();
>>>         }
>>>     }
>>>
>>> Thanks,
>>> Pankaj
>>>
>>
>>
> 
> 


RE: multipart/form-data support

Posted by "Caldarale, Charles R" <Ch...@unisys.com>.
> From: Pankaj Tiwari [mailto:panky.tiwari@gmail.com] 
> Subject: Re: multipart/form-data support

> 2. The request never reaches the server.

Then it can't be a Tomcat issue, can it?

 - Chuck


THIS COMMUNICATION MAY CONTAIN CONFIDENTIAL AND/OR OTHERWISE PROPRIETARY MATERIAL and is thus for use only by the intended recipient. If you received this in error, please contact the sender and delete the e-mail and its attachments from all computers.


Re: multipart/form-data support

Posted by Pankaj Tiwari <pa...@gmail.com>.
1. Keep-Alive - I am not sure if that should cause any issues here.
2. The request never reaches the server. I have tried without the last
connect as well but nothing seems to work here.

When I run this piece of code, I do not get any exceptions or error on the
client side but the request does not seems to be reaching the server.
(doGet/doPost methods are never called).

Q1. How can i debug if the request is being sent from the java client at
all? If it had been a browser, I could have used a header monitoring plugin
but not sure about java application
Q2. Is there any chance that tomcat does not support or ignores such request
(multipart file-data)?

On Mon, Jan 3, 2011 at 7:53 PM, Pid <pi...@pidster.com> wrote:

> On 1/3/11 3:06 AM, Pankaj Tiwari wrote:
> > I have been trying to send multipart/form-data to the server. I have
> found
> > that the request never reaches the server.
>
> Doesn't sound like a Tomcat problem to me then...
>
> > This is my client code, if I am missing something core in here.
> >
> >     public static void main(String args[])
> >     {
> >         try
> >         {
> >             URL url = new URL(SERVERURL);
> >             HttpURLConnection con = (HttpURLConnection)
> > url.openConnection();
> >
> >             con.setDoInput(true);
> >             con.setDoOutput(true);
> >             con.setUseCaches(false);
> >
> >             con.setRequestProperty("Content-Type",
> > "multipart/form-data;boundary=" + BOUNDARY);
> >             con.setRequestMethod("POST");
>
> Why are you using keep alive?
>
> >             con.setRequestProperty("Connection", "Keep-Alive");
> >
> >             OutputStream out = con.getOutputStream();
>
> Why are you using a DataOutputStream?
>
> >             DataOutputStream oos = new DataOutputStream(out);
> >             oos.writeBytes(TWOHYPHENS + BOUNDARY + LINEEND);
> >             oos.writeBytes("Content-Disposition: form-data;
> name=\"name\"" +
> > LINEEND);
> >             oos.writeBytes(LINEEND);
> >             oos.write("HI".getBytes("UTF-8"));
> >             oos.writeBytes(LINEEND);
> >             oos.writeBytes(TWOHYPHENS + BOUNDARY + TWOHYPHENS + LINEEND);
> >             oos.flush();
> >             con.connect();
>
> Why are you connecting after you've attempted to write data?
>
>
> p
>
> >             oos.close();
> >         }
> >         catch (MalformedURLException e)
> >         {
> >             e.printStackTrace();
> >         }
> >         catch (IOException e)
> >         {
> >             e.printStackTrace();
> >         }
> >     }
> >
> > Thanks,
> > Pankaj
> >
>
>


-- 
Pankaj Tiwari

Re: multipart/form-data support

Posted by Pid <pi...@pidster.com>.
On 1/3/11 3:06 AM, Pankaj Tiwari wrote:
> I have been trying to send multipart/form-data to the server. I have found
> that the request never reaches the server.

Doesn't sound like a Tomcat problem to me then...

> This is my client code, if I am missing something core in here.
> 
>     public static void main(String args[])
>     {
>         try
>         {
>             URL url = new URL(SERVERURL);
>             HttpURLConnection con = (HttpURLConnection)
> url.openConnection();
> 
>             con.setDoInput(true);
>             con.setDoOutput(true);
>             con.setUseCaches(false);
> 
>             con.setRequestProperty("Content-Type",
> "multipart/form-data;boundary=" + BOUNDARY);
>             con.setRequestMethod("POST");

Why are you using keep alive?

>             con.setRequestProperty("Connection", "Keep-Alive");
> 
>             OutputStream out = con.getOutputStream();

Why are you using a DataOutputStream?

>             DataOutputStream oos = new DataOutputStream(out);
>             oos.writeBytes(TWOHYPHENS + BOUNDARY + LINEEND);
>             oos.writeBytes("Content-Disposition: form-data; name=\"name\"" +
> LINEEND);
>             oos.writeBytes(LINEEND);
>             oos.write("HI".getBytes("UTF-8"));
>             oos.writeBytes(LINEEND);
>             oos.writeBytes(TWOHYPHENS + BOUNDARY + TWOHYPHENS + LINEEND);
>             oos.flush();
>             con.connect();

Why are you connecting after you've attempted to write data?


p

>             oos.close();
>         }
>         catch (MalformedURLException e)
>         {
>             e.printStackTrace();
>         }
>         catch (IOException e)
>         {
>             e.printStackTrace();
>         }
>     }
> 
> Thanks,
> Pankaj
>