Click to See Complete Forum and Search --> : Simple HTTP client... won't work :(


Znupi
11-24-2007, 05:15 PM
I'm very new to Java so please be gentle :o.
Now, I'm trying to use sockets to make a simple http client in Java. I did one in PHP in 5 minutes (okay maybe 10 :)) so I know "what I have to do". But I just can't get it to work in Java. It took me a couple of hours just to get it stop throwing tons of errors :(. I need help, please :(

HTTPClient.java

package com.znupi.http;
import java.net.*;
import java.io.*;
public class HTTPClient {
private static String host = "127.0.0.1";
public static void main(String[] args) {
Socket socket = null;
PrintWriter socket_out = null;
BufferedReader socket_in = null;
String response = null;
char[] buf = new char[1024];
int charNo = -1;
System.out.println("Starting...");
try {
socket = new Socket(host, 80);
}
catch (UnknownHostException e){
System.err.println("UnknownHostException: " + e);
System.exit(1);
}
catch (IOException e) {
System.err.println("IOException: " + e);
System.exit(1);
}
try {
socket_out = new PrintWriter(socket.getOutputStream(), true);
}
catch (IOException e) {
System.err.println("IOException: " + e);
System.exit(1);
}
try {
socket_in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
}
catch (IOException e) {
System.err.println("IOException: " + e);
System.exit(1);
}
String toSend = "GET /work3/test.php HTTP/1.1\r\nHost: " + host + "\r\nConnection: close\r\n\r\n";
System.out.println("Sending headers:\r\n" + toSend);
socket_out.print(toSend);
System.out.println("Sent.");
do {
try {
charNo = socket_in.read(buf);
}
catch (IOException e) {
System.err.println("IOException: " + e);
}
if (charNo != -1) {
System.out.print(buf);
response += buf;
}
}
while (charNo != -1);
System.out.print(response);
socket_out.close();
try {
socket_in.close();
}
catch (IOException e) {
System.err.println("IOException: " + e);
System.exit(1);
}
try {
socket.close();
}
catch (IOException e) {
System.err.println("IOException: " + e);
System.exit(1);
}
}
}

/work3/test.php just writes another '.' to test.txt... when accessed with a browser.
And here's the output that I get:

Starting...
Sending headers:
GET /work3/test.php HTTP/1.1
Host: 127.0.0.1
Connection: close


Sent.

And then it hangs (it doesn't terminate).
I just can't see why it doesn't work :(. Please help :(. And thank you in advance :)

chazzy
11-24-2007, 06:22 PM
Probably, while possibly, using a Socket is making it much more difficult than it needs to be.

Maybe you should start by using the URL class? Here's a little sample code, that does essentially the same thing..


URL urlEP = new URL("http://127.0.0.1/work3/test.php");
InputStream is = urlEP.openStream();
BufferedReader br = new BufferedReader(is);
String output = br.readLine();

Znupi
11-24-2007, 06:31 PM
Well, I thought that there would probably be some special class to handle http requests, but I didn't care to look for it because I want to make this espacially using sockets, because actually I have another project on my mind that requires sockets and this was just a test to try and get sockets working :(...

Znupi
11-24-2007, 06:48 PM
Ok I got it working... Here's what I had to change:

String toSend = "GET / HTTP/1.1\r\n" +
"Host: " + host + "\r\n" +
"Connection: close\r\n" +
"\r\n";
System.out.println("Sending headers:\r\n" + toSend);
socket_out.println(toSend);
System.out.println("Sent.");
do {
try {
buf = socket_in.readLine();
}
catch(IOException e) {
System.err.println("IOException: " + e);
System.exit(1);
}
if (buf != null) {
response += buf + "\r\n";
}
}

It seems that the main problem was that I was calling socket_out.print instead of socket_out.println. Can anyone please explain why print() wouldn't work? :(

Znupi
11-25-2007, 09:07 AM
Found the problem. When using socket_out.print() you also have to call socket_out.flush() after sending data. :)

Java is finally starting to be nice to me :)