|
|||||||
|
|
|
![]() |
|
|
Strumenti |
|
|
#1 |
|
Junior Member
Iscritto dal: Feb 2006
Messaggi: 21
|
[java] non ne esco più... gestione POST HTTP in un httpserver
Ciao a tutti!
Sono abbastanza distrutta da un errore che non riesco a risolvere nel semplice server http che sto sviluppando come progetto per un esame in università. Il server è costituito da tre classi: -HttpServer che contiene anche il main, ed è la classe che gestisce tutto il processo; -Richiesta, la classe che fa il parsing dell'header che riceve dal client e riconosce la richiesta http fatta dal client (devo supportare richieste GET, HEAD e POST); -Risposta, la classe che crea la risposta opportuna a inviare al client. Il mio problema è nella classe Richiesta; quando gestisco la POST, infatti, voglio anche salvare il body della richiesta per non meglio specificati "usi futuri"; ho impostato questo codice: Codice:
public void servi() {
StringTokenizer s = null;
String temp = null;
String fileName = null;
String metodoDaServire = null;
try {
while(br.ready()) {
String headerLine = null;
try {
headerLine = br.readLine();
} catch (IOException e2) {
System.out.println("Problemi nella lettura dello stream dati in ingresso");
e2.printStackTrace();
}
//stampa a video dell'header ricevuto
System.out.println(headerLine);
//fine header se...
if(headerLine.equals(CRLF) || headerLine.equals("")) {
break;
}
//serve a riconoscere il tipo di richiesta
s = new StringTokenizer(headerLine);
temp = s.nextToken();
if ((temp.equals("GET"))||(temp.equals("POST"))||(temp.equals("HEAD"))) {
metodoDaServire = temp;
fileName = s.nextToken();
}
}
} catch (IOException e) {
System.out.println("Stream dati non pronto");
e.printStackTrace();
}
//salva il corpo della richiesta POST
if (metodoDaServire.equals("POST")) {
try {
String postLine = br.readLine();
System.out.println(postLine);
} catch (IOException e) {
System.out.println("L'errore è qui?");
e.printStackTrace();
}
}
//crea la risposta alla richiesta fatta dal client
rispostaLocale.creaRisposta(metodoDaServire, fileName);
try {
input.close();
br.close();
socket.close();
} catch (IOException e) {
System.out.println("Problemi nella chiusura degli stream dati in ingresso");
e.printStackTrace();
}
}
}
Codice:
//salva il corpo della richiesta POST
if (metodoDaServire.equals("POST")) {
try {
String postLine = br.readLine();
System.out.println(postLine);
} catch (IOException e) {
System.out.println("L'errore è qui?");
e.printStackTrace();
}
Qualcuno ha un'idea del perché mi succede? (ho provato anche a leggerlo a caretteri come InputStreamReader ma si "blocca" lo stesso...) O mi sapete suggerire una strada più corretta e che funzioni per risolvere il problema?? Scusate il papiro ma è lunga da spiegare ed anche così non so se mi sono spiegata bene... Grazie a chiunque per consigli e suggerimenti!!!!
__________________
"Computer Science is no more about computers than astronomy is about telescopes" (Edsger Dijkstra) |
|
|
|
|
| Strumenti | |
|
|
Tutti gli orari sono GMT +1. Ora sono le: 04:50.



















