• Post Reply Bookmark Topic Watch Topic
  • New Topic

How to send POST data to a server with a HttpURLConnection

 
SkyTonic
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi folks,

nice Website with many solutions for many problems, but I didn't find anything about my problem yet.


I want to add a weather info on my homepage and for this I want to use a present weather site from the internet.
But on my site, I want my own layout!
So what I want to do is, on my site you choose the town or city from which you want to know the weather. By clicking on Submit you get to a getResult.jsp also on my Server.
This file has to contact the weather service (differnet server), which has a form tag same as mine, and this one wants POST data. (the city)

So how can I fill the POST data via Java? If it would be GET it would be no problem, because I can contact the page with a HttpURLConnection and interpret the result, by adding the data behind the URL.
(e.g.: www....com?city=NewYork)

But as I already said, the site wants POST data and I don't know where to put it.

Please help me, I'm searching for several days now and please make your answers simple, because I'm from germany and my english is not so good.



Greetz
SkyTonic
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
HttpUrlConnection has a setRequestMethod() method which allows you to indicate that this is to be a POST type of request.
http://java.sun.com/j2se/1.5.0/docs/api/java/net/HttpURLConnection.html
You'll have to build the request body yourself.

You may want to look into:
http://jakarta.apache.org/commons/httpclient
It's a higher level framework and will make the task a little easier.
 
Nathan Pruett
Bartender
Posts: 4121
IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The easiest way would probably be to use the Jakarta Commons HttpClient package. Or you could find out the structure of a HTTP POST message and build it all yourself...

By the way, unfortunately "Sky Hawaii" does not meet the JavaRanch Naming Policy. User names cannot be obviously fake and must constist of a first name and a last name.

You can change your user name here.

Thanks! and welcome to the JavaRanch!
 
SkyTonic
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

You may want to look into:
http://jakarta.apache.org/commons/httpclient
It's a higher level framework and will make the task a little easier.


This was what I was searching for, thanks for the fast answer, I hope I'll get it done with that.


Greetz
SkyTonic
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!