Http Client/Server Problem

Hi all,

I am trying to write an application that communicates over the network using HTTP. I have written two little classes. One to send data using an HttpConnection (Client) and the other to listen using a ServerSocket (Server).

My problem is that although the Http Header info comes through to the ServerSocket the Content never shows up and even though the Server says it is repsonding the Client never gets it. Does anyone know what I'm doing wrong?

This is the class that sends the Data :

-

import java.net.*;

import java.io.*;

public class HttpSend

{

/** Creates new HttpSend() instance */

public HttpSend()

{

}

/** Posts a String to an Internet address using HTTP Post.*/

public boolean post(String address, String content)

{

try

{

URL url = new URL(address);

HttpURLConnection http = (HttpURLConnection)url.openConnection();

http.setDoInput(true);

http.setDoOutput(true);

http.setUseCaches(false);

http.setRequestMethod("POST");

http.connect();

System.out.println("Client : Connected");

DataOutputStream out = new DataOutputStream(http.getOutputStream());

System.out.println("Client : Writing Content");

out.writeBytes(content);

System.out.println("Client : Flushing Stream");

out.flush();

System.out.println("Client : Waiting for response from Server");

BufferedReader in = new BufferedReader(new InputStreamReader(http.getInputStream()));

System.out.println("Client : Opened input stream");

String input = "", response = "";

while((input = in.readLine()) != null)

{

response += input + "\r";

}

System.out.println("Client : received : "+response);

//I'm not bothering to close the streams or http connection yet.

}

catch(Exception e)

{

System.out.println("Client : Error : "+e.getMessage());

return false;

}

return true;

}

/**

* @param args the command line arguments

*/

public static void main (String args[])

{

if(args.length < 1)

{

System.out.println("HttpSend - Usage : Port number required as a Parameter.");

System.exit(0);

}

HttpSend http = new HttpSend();

boolean ok = http.post("http://glue_damian:"+args[0], "Hello World");

}

}

--

and this is the class that listens :

--

import java.net.*;

import java.io.*;

public class SocketServer

{

public static void main(String args[])

{

Socket client;

ServerSocket sock = null;

if(args.length < 1)

{

System.out.println("SocketServer - Usage : Port number required as a Parameter.");

System.exit(0);

}

try

{

sock = new ServerSocket(Integer.parseInt(args[0]));

System.out.println("Server : Server Waiting for Connections on port "+args[0]);

client = sock.accept();

System.out.println("Server : Accepted Client Socket from port "+client.getPort());

client.setSoTimeout(0);

client.setTcpNoDelay(true);

System.out.println("Server : Client is : "+client.getInetAddress().toString());

BufferedReader in = new BufferedReader(new InputStreamReader(client.getInputStream()));

PrintStream out = new PrintStream(client.getOutputStream());

while(true)

{

String input = in.readLine();

while(input != null)

{

System.out.println("Server : Received : '"+input+"'");

input = in.readLine();

if(input.equals("")) break;

}

System.out.println("Server : Finished Reading. Sending Response.");

out.println("HTTP/1.0 200 OK");

out.flush();

//out.close();

}

}

catch(IOException e)

{

System.out.println("Server : Error : "+e.getMessage());

}

}

}

-

and this is all I get as output:

-

Server : Accepted Client Socket from port 1092

Server : Client is : glue_damian/127.0.0.1

Client : Connected

Client : Writing Content

Client : Flushing Stream

Client : Waiting for response from Server

Server : Received : 'POST / HTTP/1.0'

Server : Received : 'User-Agent: Java1.2.2'

Server : Received : 'Host: glue_damian:9999'

Server : Received : 'Accept: text/html, image/gif, image/jpeg, *; q=.2, */*; q=.2'

Server : Received : 'Content-type: application/x-www-form-urlencoded'

Server : Received : 'Content-length: 11'

Server : Finished Reading. Sending Response.

[4704 byte] By [damianharvey] at [2007-9-26 2:53:53]
# 1

that break only exits the inner while loop?

are you still in the outer while( true ) loop?

> public class SocketServer

> {

>

> public static void main(String args[])

> {

> while(true)

> {

> while(input != null)

> {

> if(input.equals("")) break;

> }

> System.out.println("Server : Finished Reading. Sending

> Response.");

> }

> }

mchan0 at 2007-6-29 10:42:58 > top of Java-index,Archived Forums,Socket Programming...
# 2
Did you resolve this? I have the same problem now.Any help is welcome.
xfyang at 2007-6-29 10:42:58 > top of Java-index,Archived Forums,Socket Programming...
# 3

The problem is your are breaking after reading a blank like. An http connection sends the header information then a blank line, then the content. So if you keep the check for null in there but remove the if statement checking for a blank line you should get the desired result. Hope this helped!

hattan at 2007-6-29 10:42:58 > top of Java-index,Archived Forums,Socket Programming...