The client:
public List<String> post(List<String> toWrite){
String result = "";
List<String> allResults = new ArrayList<String>();
try {
openConnection();
connection.setDoOutput(true);
connection.setRequestMethod("POST");
initializeOutputStream();
for(int i = 0; i < toWrite.size(); i++){
out.write(toWrite.get(i));
out.newLine();
}
System.out.println(connection.getResponseCode());
System.out.println(connection.getResponseMessage());
initializeInputStream();
while((result = in.readLine()) != null){
allResults.add(result);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
closeConnection();
}
return allResults;
}
One of the attempts at the host:
protected void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
// TODO Auto-generated method stub
List<String> incoming = new ArrayList<String>();
// BufferedReader in = req.getReader();
//
// String tmp = in.readLine();
resp.setContentType("text/plain");
PrintWriter out = resp.getWriter();
StringBuilder stringBuilder = new StringBuilder();
BufferedReader bufferedReader = null;
try {
InputStream inputStream = req.getInputStream();
if (inputStream != null) {
bufferedReader = new BufferedReader(new InputStreamReader(
inputStream));
char[] charBuffer = new char[128];
int bytesRead = -1;
//while ((bytesRead = bufferedReader.read(charBuffer)) > 0) {
while(bytesRead != -1){
stringBuilder.append(charBuffer, 0, bytesRead);
}
} else {
stringBuilder.append("");
}
} catch (IOException ex) {
throw ex;
} finally {
if (bufferedReader != null) {
try {
bufferedReader.close();
} catch (IOException ex) {
throw ex;
}
}
}
String body = stringBuilder.toString();
System.out.println(body);
out.println(body);
// BufferedReader in = new BufferedReader(new InputStreamReader(req.getInputStream()));
//
// String tmp = "";
//
// //while(!(in.ready())){}
//
// while((tmp = in.readLine()) != null){
// System.out.println(tmp);
// }
//
//
// out.println(tmp);
out.println("end");
}
Please note the commented out lines- thats one of the many other attempts I've tried to get stuff from the client.
System.out.printlns and out.printlns from the servlett all return a blank line. The "end" at the end of the program returns without problem. It is not a problem of reading multiple lines back on the client side- if I put multiple out.println's, then I read them fine. The system.out.println() for the inputstream also returns blank. The status code is 200, so there seems to be no connection errors.
Anyone?