From: Mário on 9 Feb 2010 12:17 Hello, I am trying to post an object to a servlet via HTTP using the following code, but unfortunately, nothing is sent to the server: private static void doJavaPost() throws IOException { URL url = new URL("http://localhost:9999/myservlet"); URLConnection connection = url.openConnection(); connection.setDoOutput(true); ByteArrayOutputStream data = new ByteArrayOutputStream(); ObjectOutputStream out = new ObjectOutputStream(data); out.writeObject(new Date()); out.close(); OutputStream os = connection.getOutputStream(); data.writeTo(os); os.flush(); os.close(); } Does someone know why this does not generate a POST request? Regards, Mário
From: Lothar Kimmeringer on 9 Feb 2010 12:30 M�rio wrote: > I am trying to post an object to a servlet via HTTP using the > following code, > but unfortunately, nothing is sent to the server: > > private static void doJavaPost() throws IOException { > URL url = new URL("http://localhost:9999/myservlet"); > URLConnection connection = url.openConnection(); > connection.setDoOutput(true); > ByteArrayOutputStream data = new ByteArrayOutputStream(); > ObjectOutputStream out = new ObjectOutputStream(data); > out.writeObject(new Date()); > out.close(); > OutputStream os = connection.getOutputStream(); > data.writeTo(os); > os.flush(); > os.close(); > } > > Does someone know why this does not generate a POST request? Because you don't ask the connection to do that. Call connection.getInputStream() and the request will be sent. BTW: You can skip the writing to the ByteArrayOutputStream and initialized the ObjectOutputStream with connection.getOutputStream() Calling of os.close() isn't needed, either. Regards, Lothar -- Lothar Kimmeringer E-Mail: spamfang(a)kimmeringer.de PGP-encrypted mails preferred (Key-ID: 0x8BC3CD81) Always remember: The answer is forty-two, there can only be wrong questions!
From: markspace on 9 Feb 2010 12:39 M�rio wrote: > ByteArrayOutputStream data = new ByteArrayOutputStream(); .... > Does someone know why this does not generate a POST request? Even after correcting the calls as Lothar suggested, I don't see how this is going to generate a POST request, or that sending binary data to an HTTP socket is going to be a good idea. Are you sure this is really what you want to do? Could you explain a bit more what you are trying to accomplish?
From: Mário on 9 Feb 2010 13:26 On Feb 9, 5:30 pm, Lothar Kimmeringer <news200...(a)kimmeringer.de> wrote: > Mário wrote: > > I am trying to post an object to a servlet via HTTP using the > > following code, > > but unfortunately, nothing is sent to the server: > > > private static void doJavaPost() throws IOException { > > URL url = new URL("http://localhost:9999/myservlet"); > > URLConnection connection = url.openConnection(); > > connection.setDoOutput(true); > > ByteArrayOutputStream data = new ByteArrayOutputStream(); > > ObjectOutputStream out = new ObjectOutputStream(data); > > out.writeObject(new Date()); > > out.close(); > > OutputStream os = connection.getOutputStream(); > > data.writeTo(os); > > os.flush(); > > os.close(); > > } > > > Does someone know why this does not generate a POST request? > > Because you don't ask the connection to do that. Call > connection.getInputStream() and the request will be sent. > BTW: You can skip the writing to the ByteArrayOutputStream > and initialized the ObjectOutputStream with > connection.getOutputStream() > Calling of os.close() isn't needed, either. > > Regards, Lothar > -- > Lothar Kimmeringer E-Mail: spamf...(a)kimmeringer.de > PGP-encrypted mails preferred (Key-ID: 0x8BC3CD81) > > Always remember: The answer is forty-two, there can only be wrong > questions! Nice! I've added the lines InputStream in = connection.getInputStream(); in.close(); and it now works: the Date object is sent to the servlet. But, why is the binary data sent in a subsequent packet? What can be done to send a single packet for the POST request? Regards, Mário
From: Roedy Green on 9 Feb 2010 13:30 On Tue, 9 Feb 2010 09:17:34 -0800 (PST), M�rio <mljrg2(a)gmail.com> wrote, quoted or indirectly quoted someone who said : > >I am trying to post an object to a servlet via HTTP using the >following code, >but unfortunately, nothing is sent to the server: see http://mindprod.com/products1.html#HTTP for POST code that works. for an example of use see https://wush.net/websvn/mindprod/filedetails.php?repname=mindprod&path=%2Fcom%2Fmindprod%2Fposter%2FAllPostersSOAP.java -- Roedy Green Canadian Mind Products http://mindprod.com Every compilable program in a sense works. The problem is with your unrealistic expections on what it will do.
|
Next
|
Last
Pages: 1 2 Prev: Statements before super() Next: Java Lead with GUI & SWT | CA | 10+ months |