Ciao a tutti, sono nuova del forum e spero possiate darmi una mano!
Sto provando a inviare una richiesta HTTP post ad un webscript di alfresco tramite la classe HttpUrlConnection, ma non riesco a leggere i dati inviati nel webscript.
La funzione con cui effettuo il collegamento è la seguente:
--------------------------------------------------------------------
protected String connect (String url , File file, String nomeFile, String ticket){
System.out.println(url);
HttpURLConnection connection = null;
DataOutputStream dos = null;
BufferedReader rd = null;
StringBuilder sb = null;
URL serverAddress = null;
int bytesRead, bytesAvailable, bufferSize;
byte[] buffer;
//definisco un boundary
String lineEnd = "\r\n";
String twoHyphens = "-";
String boundary = "--1234567";
try {
serverAddress = new URL(url);
//set up out communications stuff
connection = null; //Set up the initial connection
connection = (HttpURLConnection)serverAddress.openConnection();
connection.setDoInput(true);
connection.setDoOutput(true);
connection.setUseCaches(false);
connection.setRequestMethod("POST");
connection.setRequestProperty("Connection", "Keep-Alive");
//Il Content-Type della form
connection.setRequestProperty("Content-Type", "multipart/form-data;boundary=" + boundary);
connection.connect();
//Apro lo streaming verso la servlet
dos = new DataOutputStream( connection.getOutputStream() );
dos.writeBytes(boundary + lineEnd);
//I dati
dos.writeBytes("Content-Disposition: form-data; name=\"nome\"" + lineEnd);
dos.writeBytes(lineEnd);
dos.writeBytes(URLEncoder.encode(nomeFile));
dos.writeBytes(lineEnd);
dos.writeBytes( boundary + lineEnd);
dos.writeBytes("Content-Disposition: form-data; name=\"step\"" + lineEnd);
dos.writeBytes(lineEnd);
dos.writeBytes(URLEncoder.encode("2"));
dos.writeBytes(lineEnd);
dos.writeBytes( boundary+ "--" + lineEnd);
dos.flush();
dos.close();
//A questo punto devo rimanere in attesa della risposta della servlet, perchè altrimenti il file non sarà caricato
try {
rd = new BufferedReader(new InputStreamReader(connection.getInputStream()));
sb = new StringBuilder();
String str;
while (( str = rd.readLine()) != null) {
System.out.println("Server response is: "+str);
System.out.println("");
sb.append(str + '\n');
}
rd.close();
}catch (IOException ioex) {
System.out.println("From (ServerResponse): "+ioex);
}
} catch (MalformedURLException e)
{
e.printStackTrace();
} catch (ProtocolException e)
{
e.printStackTrace();
} catch (IOException e)
{
e.printStackTrace();
}finally
{ //close the connection, set all objects to null
connection.disconnect();
rd = null;
connection = null;
}
return sb.toString();
}
----------------------------------------------------------------------------------------
E con httpsniffer posso vedere la richiesta inviata, eccola:
----------------------------------------------------------------------------------------
POST /alfresco/service/ricerca/uploadDoc?nome=fdhfdghfdgh&descrizione=fxghdhfh&st ep=2&alf_ticket=TICKET_ce123af85720a5a3bd1fe3eaeaf 08aa2b8ac9825 HTTP/1.1
Connection: Keep-Alive
Content-Type: multipart/form-data;boundary=--1234567
Cache-Control: no-cache
Pragma: no-cache
User-Agent: Java/1.6.0_12
Host: localhost:8080
Accept: text/html, image/gif, image/jpeg, *; q=.2, */*; q=.2
Content-Length: 145
--1234567
Content-Disposition: form-data; name="nome"
fdhfdghfdgh
--1234567
Content-Disposition: form-data; name="step"
2
--1234567--
----------------------------------------------------------------------------------------
Ora, dall'oggetto WebScriptRequest disponibile nella classe alfresco invocata quando viene fatta una richiesta al webscript provo ad accedere al contenuto della richiesta http, ma risulta vuoto:
Content cont = req.getContent();
System.out.println("Content: " + cont.getContent());
non stampa il content.![]()
![]()
Non riesco a venirne a capo, avete qualche consiglio?
Grazie!!