How can I send an HTTP Post request by Java
I looked at a few examples but they really made no sense
*cheers
Maboroshi
Send HTTP Post request [Java]
- bad_brain
- Site Owner
- Posts: 11636
- Joined: 06 Apr 2005, 16:00
- 19
- Location: In your eye floaters.
- Contact:
good example here:
http://developers.sun.com/mobility/midp/ttips/HTTPPost/
and here is the HTTPConnection class in detail:
http://www.innovation.ch/java/HTTPClien ... ction.html
http://developers.sun.com/mobility/midp/ttips/HTTPPost/
and here is the HTTPConnection class in detail:
http://www.innovation.ch/java/HTTPClien ... ction.html
It's just like any other socket connection using Java, then it's simply to follow the http protocol =)
I wrote you a small example here, it sends a request to google and prints the result.
EDIT: I wrote it fast, so tell me if you want comments and I might be able to add that later tonight.
I wrote you a small example here, it sends a request to google and prints the result.
Code: Select all
import java.net.*;
import java.io.*;
public class httpReq
{
public static void main(String[] args)
{
String receive = "";
try {
Socket clientSocket = new Socket("google.com", 80);
PrintWriter out = new PrintWriter(clientSocket.getOutputStream(), true);
BufferedReader in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
out.println("GET /index.html HTTP/1.1");
out.println("Host: google.com");
out.println("Connection: Close");
out.println();
while((receive = in.readLine()) != null)
{
System.out.println(receive);
}
out.close();
in.close();
} catch(UnknownHostException e) {
System.err.print("Couldn't establish a connection\n");
System.exit(1);
}
catch(IOException e) {
System.err.print("You have encountered an I/O error, drop down and give me 20\n");
System.exit(1);
}
}
}
"The best place to hide a tree, is in a forest"