Sono andato a leggere la documentazione ufficiale (rfc 1867) per implementare un http client che supporti il multipart/form-data (quello di cui ho bisogno perchè devo mandare al server sia text field sia file) e seguento le istruzioni contenuto nel documento ho tirato giù questo script di esempio:
codice:
public class TestHttp {
public static void main(String[] args) {
String boundary = "Aax095f";
String hostname = "localhost";
try {
Socket s = new Socket(hostname,80);
InputStream in = s.getInputStream();
OutputStream out = s.getOutputStream();
String content = "Content-Disposition: form-data; name=\"nome\"\r\n\r\n";
content += "Lele\r\n";
content += "--"+boundary+"--\r\n";
byte[] contentByte = content.getBytes();
out.write("POST /prova.php HTTP/1.0\r\n".getBytes());
out.write(("Host: "+hostname+"\r\n").getBytes());
out.write(("Content-Type: multipart/form-data; boundary="+boundary+"\r\n").getBytes());
out.write(("Content-Lenght: "+contentByte.length+"\r\n\r\n").getBytes());
out.write(("--"+boundary+"\r\n").getBytes());
out.write(contentByte);
out.write("\r\n".getBytes());
int c = 0;
while ((c = in.read()) != -1) {
System.out.print((char)c);
}
} catch (Exception e) {
System.err.println(e);
}
}
}
Quando lo eseguo il server risponde restituendomi la pagina creata da prova.php ma non riesce a leggere la variabile post nome.
Cosa c'è che non va? Le sto provando tutte!!
Grazie
Lele