dcsimg
Java Http Server and Http Client
1 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   Sanjit_Singh
Posted On:   Monday, November 4, 2002 02:42 AM

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 "); while(true) { Socket clientSocket = serverSocket.accept(); System.out.println(clientSocket.getInetAddress()   More>>

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
");



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
";

response = response + "Date: Fri, 04 May 2001 20:08:11 GMT
";

response = response + "Server: Sanjits Server
";

response = response + "Connection: close
";

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 = " Hello ";

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!

   <<Less

Re: Java Http Server and Http Client

Posted By:   Scott_Yaung  
Posted On:   Monday, November 4, 2002 07:25 AM

I think maybe something like these.

Since u use reader and readline, it

means it will wait till it meet a return"
" or "
".

but ur client just send "...." no "
" or "
"

followed, so ur server will keep on waiting.

u can try add a "
" after send the xml format data.



Hope I am right :-)
About | Sitemap | Contact