This week's book giveaway is in the JavaScript forum.
We're giving away four copies of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js and have Paul Jensen on-line!
See this thread for details.
Win a copy of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Using a Java URL object to POST to Servlet?  RSS feed

 
Mike London
Ranch Hand
Posts: 1441
8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can you use a Java URL object to send data to a Servlet to the POST Method?

I have a regular Java application (with main method) that I want to send a ByteArrayOutputStream to the POST method of a Servlet at a specific URL.

On the face of it, its seems like a URL object would work OK. I've used a URL object to send data to a regular server class, but not (yet) to a Servlet's doPost() method.

So, would the URL object work with POSTing data to a Servlet?

Thanks in advance.

-Mike
 
Paul Clapham
Sheriff
Posts: 22374
42
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sure. The google keywords for that would be "java url tutorial" and that should get you on the way.

Although the idea of sending a ByteArrayOutputStream is a bit peculiar. Normally you would just send the bytes to the servlet, instead of sending them somewhere else (the B-A-O-S) first.
 
Mike London
Ranch Hand
Posts: 1441
8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul Clapham wrote:Sure. The google keywords for that would be "java url tutorial" and that should get you on the way.

Although the idea of sending a ByteArrayOutputStream is a bit peculiar. Normally you would just send the bytes to the servlet, instead of sending them somewhere else (the B-A-O-S) first.


OK, thanks!

I was just confirming.

(Sending a BAOS was more notional than literal.)

Appreciate your reply!!!

mike
 
Mike London
Ranch Hand
Posts: 1441
8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul Clapham wrote:Sure. The google keywords for that would be "java url tutorial" and that should get you on the way.

Although the idea of sending a ByteArrayOutputStream is a bit peculiar. Normally you would just send the bytes to the servlet, instead of sending them somewhere else (the B-A-O-S) first.



*** Paul, ***

OK, I created a URL object with the logic from the Java tutorial, but it's not connecting at all to the Servlet.

The code is mostly below. The servlet currently works with the way I'm sending two values as shown below. However, using a URL object, the logging statement, which is one of the first lines in the POST, never logs anything.

In the URL code below, there's nothing to indicate this is a POST method or to set the ContentType.

Are you sure this should POST to a Servlet?

Look forward to your reply. :)

- Mike

 
Paul Clapham
Sheriff
Posts: 22374
42
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If I'm not mistaken line 06 in that code makes it a POST request. As for the content type, that would be a header which you can set.

But if it's not even connecting to the servlet then maybe you used the wrong URL? Or you don't have network connectivity to the server? You can tell a lot by the HTTP return code and by stack traces of any exceptions which are thrown.
 
Mike London
Ranch Hand
Posts: 1441
8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul Clapham wrote:If I'm not mistaken line 06 in that code makes it a POST request. As for the content type, that would be a header which you can set.

But if it's not even connecting to the servlet then maybe you used the wrong URL? Or you don't have network connectivity to the server? You can tell a lot by the HTTP return code and by stack traces of any exceptions which are thrown.


OK, so it "should" work. It seems to get the connection and it's the exact same URL as with another program that does the POST. However, in the .NET case, POST is explicitly mentioned and it's a byte[] type. Not that that should necessarily matter, but I've verified all the obvious stuff already.

In any case, I get no errors, same URL, etc..

I'm stumped.

- mike
 
Lester Burnham
Rancher
Posts: 1337
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think you'll have to process (i.e., read) the input stream as well, otherwise the request will not be sent. (Yes, it's counter-intuitive.)
 
Mike London
Ranch Hand
Posts: 1441
8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Lester Burnham wrote:I think you'll have to process (i.e., read) the input stream as well, otherwise the request will not be sent. (Yes, it's counter-intuitive.)


YES THIS WAS IT!!!

Once I read back in the data, all worked fine!!!

THANK YOU!!!

-mike

--------------

Ulf -- sorry about the double posting. It was an accident. I found an old message and replied to that one since it had more history.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!