• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Rob Spoor
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Henry Wong
  • Liutauras Vilda
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Tim Holloway
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Mikalai Zaikin
  • Piet Souris

HTTP Client/Server Problem

 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Damian Harvey
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No worries. Found the problems.
1. I wasn't sending a CONTINUE after receiving the header
and
2. I wasn't including carriage-return/line-feeds
Works fine now.
Regards,
Damian.
 
This is my favorite show. And this is my favorite tiny ad:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
reply
    Bookmark Topic Watch Topic
  • New Topic