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 :)
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 :)