POST-ing data to Http server

Discussion in 'Java' started by Jonathan, Jan 16, 2004.

  1. Jonathan

    Jonathan Guest

    Hi,
    I'm trying to post (using POST method) form-like data (Let's say
    parameter name = X; Value=ABC) from a Java program to a server, but it
    doesn't seem to work. Any help would be greatly appreciated. This is
    the code I'm using :

    String data = "X=ABC";
    URL u = new URL(url);
    URLConnection connection = u.openConnection();
    connection.setDoOutput(true);
    connection.setDoInput(true);
    connection.setUseCaches(false);
    connection.setAllowUserInteraction(false);
    connection.setRequestProperty("Content-type","application/x-www-form-urlencoded");
    ((HttpURLConnection) connection).setRequestMethod("POST");
    connection.setRequestProperty("Content-Length", "" + data.length());
    BufferedWriter out = new BufferedWriter(new
    OutputStreamWriter(connection.getOutputStream()));
    out.write(data);
    out.flush();
    out.close();
    connection.connect();

    Thanks for your help!

    Jonathan
     
    Jonathan, Jan 16, 2004
    #1
    1. Advertising

  2. (Jonathan) wrote in message news:<>...
    > Hi,
    > I'm trying to post (using POST method) form-like data (Let's say
    > parameter name = X; Value=ABC) from a Java program to a server, but it
    > doesn't seem to work. Any help would be greatly appreciated. This is
    > the code I'm using :
    >
    > String data = "X=ABC";
    > URL u = new URL(url);
    > URLConnection connection = u.openConnection();
    > connection.setDoOutput(true);
    > connection.setDoInput(true);
    > connection.setUseCaches(false);
    > connection.setAllowUserInteraction(false);
    > connection.setRequestProperty("Content-type","application/x-www-form-urlencoded");
    > ((HttpURLConnection) connection).setRequestMethod("POST");
    > connection.setRequestProperty("Content-Length", "" + data.length());
    > BufferedWriter out = new BufferedWriter(new
    > OutputStreamWriter(connection.getOutputStream()));
    > out.write(data);
    > out.flush();
    > out.close();
    > connection.connect();
    >
    > Thanks for your help!
    >
    > Jonathan


    String ENCODING = "UTF-8";


    try {
    URL url = new URL("http://www.website.com");
    HttpURLConnection httpURLConnection =
    (HttpURLConnection)url.openConnection();
    httpURLConnection.setUseCaches(false);
    httpURLConnection.setDoOutput(true);
    httpURLConnection.setDoInput(true);
    httpURLConnection.setRequestMethod("POST");
    httpURLConnection.setRequestProperty("Content-Type",
    "application/x-www-form-urlencoded");
    StringBuffer sbContent = new StringBuffer();
    sbContent.append("X=");
    sbContent.append(URLEncoder.encode("ABC", ENCODING));
    DataOutputStream stream = new
    DataOutputStream(httpURLConnection.getOutputStream());
    stream.writeBytes(sbContent.toString());
    stream.flush();
    stream.close();
    InputStream inputStream =
    httpURLConnection.getInputStream();
    inputStream.close();
    } catch (Throwable t) {
    }
     
    Gregory A. Swarthout, Jan 16, 2004
    #2
    1. Advertising

  3. Jonathan

    Jonathan Guest

    (Gregory A. Swarthout) wrote in message news:<>...
    > (Jonathan) wrote in message news:<>...
    > > Hi,
    > > I'm trying to post (using POST method) form-like data (Let's say
    > > parameter name = X; Value=ABC) from a Java program to a server, but it
    > > doesn't seem to work. Any help would be greatly appreciated. This is
    > > the code I'm using :
    > >
    > > String data = "X=ABC";
    > > URL u = new URL(url);
    > > URLConnection connection = u.openConnection();
    > > connection.setDoOutput(true);
    > > connection.setDoInput(true);
    > > connection.setUseCaches(false);
    > > connection.setAllowUserInteraction(false);
    > > connection.setRequestProperty("Content-type","application/x-www-form-urlencoded");
    > > ((HttpURLConnection) connection).setRequestMethod("POST");
    > > connection.setRequestProperty("Content-Length", "" + data.length());
    > > BufferedWriter out = new BufferedWriter(new
    > > OutputStreamWriter(connection.getOutputStream()));
    > > out.write(data);
    > > out.flush();
    > > out.close();
    > > connection.connect();
    > >
    > > Thanks for your help!
    > >
    > > Jonathan

    >
    > String ENCODING = "UTF-8";
    >
    >
    > try {
    > URL url = new URL("http://www.website.com");
    > HttpURLConnection httpURLConnection =
    > (HttpURLConnection)url.openConnection();
    > httpURLConnection.setUseCaches(false);
    > httpURLConnection.setDoOutput(true);
    > httpURLConnection.setDoInput(true);
    > httpURLConnection.setRequestMethod("POST");
    > httpURLConnection.setRequestProperty("Content-Type",
    > "application/x-www-form-urlencoded");
    > StringBuffer sbContent = new StringBuffer();
    > sbContent.append("X=");
    > sbContent.append(URLEncoder.encode("ABC", ENCODING));
    > DataOutputStream stream = new
    > DataOutputStream(httpURLConnection.getOutputStream());
    > stream.writeBytes(sbContent.toString());
    > stream.flush();
    > stream.close();
    > InputStream inputStream =
    > httpURLConnection.getInputStream();
    > inputStream.close();
    > } catch (Throwable t) {
    > }


    I've tried your suggestion but no success. I'm trying to call a
    servlet with my code. I can reach the servlet and debug. A call to
    request.getParameter("X") is returning null... also, whent I check the
    method in the request object, it's a GET!! Any help???
     
    Jonathan, Jan 19, 2004
    #3
  4. Jonathan

    Guest

    On 19 Jan 2004 07:10:10 -0800, (Jonathan) wrote:

    > (Gregory A. Swarthout) wrote in message news:<>...
    >> (Jonathan) wrote in message news:<>...
    >> > Hi,
    >> > I'm trying to post (using POST method) form-like data (Let's say
    >> > parameter name = X; Value=ABC) from a Java program to a server, but it
    >> > doesn't seem to work. Any help would be greatly appreciated. This is
    >> > the code I'm using :
    >> >
    >> > String data = "X=ABC";
    >> > URL u = new URL(url);
    >> > URLConnection connection = u.openConnection();
    >> > connection.setDoOutput(true);
    >> > connection.setDoInput(true);
    >> > connection.setUseCaches(false);
    >> > connection.setAllowUserInteraction(false);
    >> > connection.setRequestProperty("Content-type","application/x-www-form-urlencoded");
    >> > ((HttpURLConnection) connection).setRequestMethod("POST");
    >> > connection.setRequestProperty("Content-Length", "" + data.length());
    >> > BufferedWriter out = new BufferedWriter(new
    >> > OutputStreamWriter(connection.getOutputStream()));
    >> > out.write(data);
    >> > out.flush();
    >> > out.close();
    >> > connection.connect();
    >> >
    >> > Thanks for your help!
    >> >
    >> > Jonathan




    >>
    >> String ENCODING = "UTF-8";
    >>
    >>
    >> try {
    >> URL url = new URL("http://www.website.com");
    >> HttpURLConnection httpURLConnection =
    >> (HttpURLConnection)url.openConnection();
    >> httpURLConnection.setUseCaches(false);
    >> httpURLConnection.setDoOutput(true);
    >> httpURLConnection.setDoInput(true);
    >> httpURLConnection.setRequestMethod("POST");
    >> httpURLConnection.setRequestProperty("Content-Type",
    >> "application/x-www-form-urlencoded");
    >> StringBuffer sbContent = new StringBuffer();
    >> sbContent.append("X=");
    >> sbContent.append(URLEncoder.encode("ABC", ENCODING));
    >> DataOutputStream stream = new
    >> DataOutputStream(httpURLConnection.getOutputStream());
    >> stream.writeBytes(sbContent.toString());
    >> stream.flush();
    >> stream.close();
    >> InputStream inputStream =
    >> httpURLConnection.getInputStream();
    >> inputStream.close();
    >> } catch (Throwable t) {
    >> }

    >
    >I've tried your suggestion but no success. I'm trying to call a
    >servlet with my code. I can reach the servlet and debug. A call to
    >request.getParameter("X") is returning null... also, whent I check the
    >method in the request object, it's a GET!! Any help???



    Sounds like the Servlet you are calling does not have the getPost()
    method implemented? Can you check that? If the servlet does not have
    the POST handling methods, then you will only be able to use the GET
    handling. Sounds simple, but many people forget to implement the one
    they need. Anyway, hope this helps.
     
    , Jan 19, 2004
    #4
  5. Jonathan

    Berlin Brown Guest

    wrote:
    > On 19 Jan 2004 07:10:10 -0800, (Jonathan) wrote:
    >
    >
    >> (Gregory A. Swarthout) wrote in message news:<>...
    >>
    >>> (Jonathan) wrote in message news:<>...
    >>>
    >>>>Hi,
    >>>>I'm trying to post (using POST method) form-like data (Let's say
    >>>>parameter name = X; Value=ABC) from a Java program to a server, but it
    >>>>doesn't seem to work. Any help would be greatly appreciated. This is
    >>>>the code I'm using :
    >>>>
    >>>>String data = "X=ABC";
    >>>>URL u = new URL(url);
    >>>>URLConnection connection = u.openConnection();
    >>>>connection.setDoOutput(true);
    >>>>connection.setDoInput(true);
    >>>>connection.setUseCaches(false);
    >>>>connection.setAllowUserInteraction(false);
    >>>>connection.setRequestProperty("Content-type","application/x-www-form-urlencoded");
    >>>>((HttpURLConnection) connection).setRequestMethod("POST");
    >>>>connection.setRequestProperty("Content-Length", "" + data.length());
    >>>>BufferedWriter out = new BufferedWriter(new
    >>>>OutputStreamWriter(connection.getOutputStream()));
    >>>>out.write(data);
    >>>>out.flush();
    >>>>out.close();
    >>>>connection.connect();
    >>>>
    >>>>Thanks for your help!
    >>>>
    >>>>Jonathan

    >
    >
    >
    >
    >>> String ENCODING = "UTF-8";
    >>>
    >>>
    >>> try {
    >>> URL url = new URL("http://www.website.com");
    >>> HttpURLConnection httpURLConnection =
    >>>(HttpURLConnection)url.openConnection();
    >>> httpURLConnection.setUseCaches(false);
    >>> httpURLConnection.setDoOutput(true);
    >>> httpURLConnection.setDoInput(true);
    >>> httpURLConnection.setRequestMethod("POST");
    >>> httpURLConnection.setRequestProperty("Content-Type",
    >>>"application/x-www-form-urlencoded");
    >>> StringBuffer sbContent = new StringBuffer();
    >>> sbContent.append("X=");
    >>> sbContent.append(URLEncoder.encode("ABC", ENCODING));
    >>> DataOutputStream stream = new
    >>>DataOutputStream(httpURLConnection.getOutputStream());
    >>> stream.writeBytes(sbContent.toString());
    >>> stream.flush();
    >>> stream.close();
    >>> InputStream inputStream =
    >>>httpURLConnection.getInputStream();
    >>> inputStream.close();
    >>> } catch (Throwable t) {
    >>> }

    >>
    >>I've tried your suggestion but no success. I'm trying to call a
    >>servlet with my code. I can reach the servlet and debug. A call to
    >>request.getParameter("X") is returning null... also, whent I check the
    >>method in the request object, it's a GET!! Any help???

    >
    >
    >
    > Sounds like the Servlet you are calling does not have the getPost()
    > method implemented? Can you check that? If the servlet does not have
    > the POST handling methods, then you will only be able to use the GET
    > handling. Sounds simple, but many people forget to implement the one
    > they need. Anyway, hope this helps.
    >
    >


    Another question, what happens when you leave out the POST, I have code
    that works and for some reason gives me proper results, when I include
    POST, the code is slower and doesnt connect properly, weird.
     
    Berlin Brown, Jan 30, 2004
    #5
  6. Jonathan

    Berlin Brown Guest

    wrote:
    > On 19 Jan 2004 07:10:10 -0800, (Jonathan) wrote:
    >
    >
    >> (Gregory A. Swarthout) wrote in message news:<>...
    >>
    >>> (Jonathan) wrote in message news:<>...
    >>>
    >>>>Hi,
    >>>>I'm trying to post (using POST method) form-like data (Let's say
    >>>>parameter name = X; Value=ABC) from a Java program to a server, but it
    >>>>doesn't seem to work. Any help would be greatly appreciated. This is
    >>>>the code I'm using :
    >>>>
    >>>>String data = "X=ABC";
    >>>>URL u = new URL(url);
    >>>>URLConnection connection = u.openConnection();
    >>>>connection.setDoOutput(true);
    >>>>connection.setDoInput(true);
    >>>>connection.setUseCaches(false);
    >>>>connection.setAllowUserInteraction(false);
    >>>>connection.setRequestProperty("Content-type","application/x-www-form-urlencoded");
    >>>>((HttpURLConnection) connection).setRequestMethod("POST");
    >>>>connection.setRequestProperty("Content-Length", "" + data.length());
    >>>>BufferedWriter out = new BufferedWriter(new
    >>>>OutputStreamWriter(connection.getOutputStream()));
    >>>>out.write(data);
    >>>>out.flush();
    >>>>out.close();
    >>>>connection.connect();
    >>>>
    >>>>Thanks for your help!
    >>>>
    >>>>Jonathan

    >
    >
    >
    >
    >>> String ENCODING = "UTF-8";
    >>>
    >>>
    >>> try {
    >>> URL url = new URL("http://www.website.com");
    >>> HttpURLConnection httpURLConnection =
    >>>(HttpURLConnection)url.openConnection();
    >>> httpURLConnection.setUseCaches(false);
    >>> httpURLConnection.setDoOutput(true);
    >>> httpURLConnection.setDoInput(true);
    >>> httpURLConnection.setRequestMethod("POST");
    >>> httpURLConnection.setRequestProperty("Content-Type",
    >>>"application/x-www-form-urlencoded");
    >>> StringBuffer sbContent = new StringBuffer();
    >>> sbContent.append("X=");
    >>> sbContent.append(URLEncoder.encode("ABC", ENCODING));
    >>> DataOutputStream stream = new
    >>>DataOutputStream(httpURLConnection.getOutputStream());
    >>> stream.writeBytes(sbContent.toString());
    >>> stream.flush();
    >>> stream.close();
    >>> InputStream inputStream =
    >>>httpURLConnection.getInputStream();
    >>> inputStream.close();
    >>> } catch (Throwable t) {
    >>> }

    >>
    >>I've tried your suggestion but no success. I'm trying to call a
    >>servlet with my code. I can reach the servlet and debug. A call to
    >>request.getParameter("X") is returning null... also, whent I check the
    >>method in the request object, it's a GET!! Any help???

    >
    >
    >
    > Sounds like the Servlet you are calling does not have the getPost()
    > method implemented? Can you check that? If the servlet does not have
    > the POST handling methods, then you will only be able to use the GET
    > handling. Sounds simple, but many people forget to implement the one
    > they need. Anyway, hope this helps.
    >
    >


    I guess I will have to look at the java source again.
     
    Berlin Brown, Jan 30, 2004
    #6
  7. "Berlin Brown" ...
    > wrote:

    ....
    > > Sounds like the Servlet you are calling does not have the getPost()
    > > method implemented? Can you check that?

    ...
    > I guess I will have to look at the java source again.


    Above is an example of how you
    _might_ have responded to that.

    Note Berlin, that to put your short reply
    after a very long message, with no trimming,
    is actually the worst of all worlds.

    Please trim irrelevant text in future.

    --
    Andrew Thompson
    * http://www.PhySci.org/ Open-source software suite
    * http://www.PhySci.org/codes/ Web & IT Help
    * http://www.1point1C.org/ Science & Technology
     
    Andrew Thompson, Jan 31, 2004
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Corey Ellsworth

    "POST"ing Image Data to ASPX File?

    Corey Ellsworth, Jun 12, 2006, in forum: ASP .Net
    Replies:
    2
    Views:
    729
    Corey Ellsworth
    Jun 12, 2006
  2. Dan Greenblatt

    'post'ing Tkinter menus on windows.....

    Dan Greenblatt, Aug 13, 2004, in forum: Python
    Replies:
    0
    Views:
    279
    Dan Greenblatt
    Aug 13, 2004
  3. Replies:
    12
    Views:
    655
    Paul Rubin
    Jun 19, 2011
  4. Chris

    question about webservices and "post"ing data

    Chris, Dec 5, 2005, in forum: ASP .Net Web Services
    Replies:
    1
    Views:
    119
  5. Anon
    Replies:
    4
    Views:
    397
    schruthensis
    Jan 10, 2006
Loading...

Share This Page