Hi,
I am trying to write my own
java client and a java http server. My main aim is to send an XML document in the body of http and in return get a sequence ID from my own http server. The code for both client and server is as follows:
***********SERVER**********************
public class Server {
public static void main(
String[] args) {
try {
ServerSocket serverSocket = new ServerSocket(12001);
System.out.println("HTTP Server (only POST implemented) is ready and is listening on Port Number 12001 \n");
while(true) {
Socket clientSocket = serverSocket.accept();
System.out.println(clientSocket.getInetAddress().toString() + " " + clientSocket.getPort());
BufferedReader in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
OutputStream out = clientSocket.getOutputStream();
String temp;
while((temp=in.readLine()) != null)
System.out.println(temp);
String response = "HTTP/1.1 200 OK\n\r";
response = response + "Date: Fri, 04 May 2001 20:08:11 GMT\n\r";
response = response + "Server: Sanjits Server\n\r";
response = response + "Connection: close\n\r";
response = response + "1";
byte[] bytes = response.getBytes();
out.write(bytes);
out.flush();
in.close();
out.close();
}
} catch(Exception e) {
System.out.println("ERROR: " + e.getMessage());
System.exit(1);
}}}
********************************************************* **********CLIENT***************************************
public class Client {
public static void main(String[] args) {
try {
URL url = new URL("http://localhost:12001");
HttpURLConnection con = (HttpURLConnection) url.openConnection();
con.setDoOutput(true);
con.setDoInput(true);
con.setRequestMethod("POST");
con.setUseCaches(false);
String
test = "<name>Hello</name>";
byte[] bytes = test.getBytes();
con.setRequestProperty("Content-length", String.valueOf(bytes.length));
con.setRequestProperty("Content-type", "text/html");
OutputStream out = con.getOutputStream();
out.write(bytes);
out.flush();
BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
String temp;
while((temp = in.readLine()) != null)
System.out.println(temp);
out.close();
in.close();
con.disconnect();
} catch(Exception e) {
e.printStackTrace();
System.exit(1);}}}
*********************************************************** The output on the server side is as follows
HTTP Server (only POST implemented) is ready and is HTTP Server (only POST implemented) is ready and is listening on Port Number 12001
127.0.0.1/127.0.0.1 1592
POST / HTTP/1.1
Content-length: 18
Content-type: text/html
User-Agent: Java1.3.1_04
Host: localhost:12001
Accept: text/html, image/gif, image/jpeg, *; q=.2, */*; q=.2
Connection: keep-alive
************************
As you can see above, the http server simply "hangs" after reading the http header. It never reaches the http body and doesnt even process the response to the client. Does anyone have any ideas how to solve this? I am stuck and any help would be appreciated. Please feel free to try the code above. Thanks in advance!