• Post Reply Bookmark Topic Watch Topic
  • New Topic

Sending HTML requests from java program

 
Andreas Bystrom
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi!
I would like to build a program that will send HTML requests to a web server to trigger actions on there.
What I need is a simple program that can pretend to be a webpage sending data to test a servlet on the web server. I will for example need code to send a request to the webserver at 1.2.3.4 and simulate that I have set an INPUT html field to "Andreas".
Is the simplest way just to build the html as a string and then open a socket to the server and send it? Is there somewhere where I can find out how such strings should look like? Or should I use some existing java html extension? Note that I don't want a servlet application.
Thanks in advance,
Andreas
 
Jason Menard
Sheriff
Posts: 6450
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm going to move this to Sockets and Internet Protocols, which is probably the best place to get an answer to your question. It sounds though like you are trying to write something to test a servlet you have written, and if this is the case, you could probably find some help in methods for unit testing Servlets over in the Testing forum.
 
Thomas Rochon
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Look at following code snippet:
String data = URLEncoder.encode("name", "UTF-8") + "=" + URLEncoder.encode("Paul", "UTF-8");

// Send the data
URL url = new URL("www.URLyouWANTtoPOSTto.com/login.php");
URLConnection conn = url.openConnection();
conn.setDoOutput(true);
OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream());
wr.write(data);
wr.flush();
wr.close();
This one is sending a post request with the key/value pair name=Paul
to the login page "www.URLyouWANTtoPOSTto.com/login.php".
Best regards
- Thomas -
 
Avi Abrami
Ranch Hand
Posts: 1141
1
Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Andreas,
If you haven't already read it, perhaps the following JavaWorld article will be of interest:
Dodge the traps hiding in the URLConnection class
Good Luck,
Avi.
 
Andreas Bystrom
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Looks like the solution is out there and should not be that hard to get. But when I'm trying out the suggestions given and reading about this, I this don't get this to work.
Heres a trace I got from my browser when I sent a POST (thisi swhat I want to with a small java program)
POST /mypages/servlet/AdminServlet?Service=TestService HTTP/1.1
Accept: image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/vnd.ms-powerpoint, application/vnd.ms-excel, application/msword, */*
Referer: http://storstark.e-horizon.se/
Accept-Language: sv
Content-Type: application/x-www-form-urlencoded
Accept-Encoding: gzip, deflate
User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0; .NET CLR 1.1.4322)
Host: storstark.e-horizon.se
Content-Length: 65
Connection: Keep-Alive
Cache-Control: no-cache
NAME=andreas&DATA=testing&Button.x=31&Button.y=6
The message is sent to ip computer.domain.se. Could someone show me how I should use URL and URLConnection to achive this? I guess some of the fields in the POST message are not important...
Regards,
Andreas
 
Andreas Bystrom
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Found out how to get it to work, just added the correct properties and everything went fine. It seems that the webserver needs some of them.
Thanks for all the help!
// Andreas
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!