cancel
Showing results for 
Search instead for 
Did you mean: 

HttpUrlConnection and webscripts

nowhere
Champ in-the-making
Champ in-the-making
Hi all,
I'm trying to make a post request to a webscript by java class HttpUrlConnection, but I get 401 response code. How can I send authentication data to webscripts? And what?
Thanks!
8 REPLIES 8

mrogers
Star Contributor
Star Contributor
http://wiki.alfresco.com/wiki/Web_Scripts#Authenticating

You need to add an alf_ticket parameter to your URL.

nowhere
Champ in-the-making
Champ in-the-making
It's a post request, I can't add parameters after url (in the form ?name=value), don't I?

mrogers
Star Contributor
Star Contributor
Yes you can add the ticket to the URL.    Or you can stick it in the HTTP header.

I'm mushing up terms here.   However I hope I'm clear.

nowhere
Champ in-the-making
Champ in-the-making
Thanks mrrogers, it works!
I was thinking it wouldn't have accepted params in URL.
Your was a great help!  Smiley Happy

nowhere
Champ in-the-making
Champ in-the-making
Now there is another issue:

I can make post request to my java backed webscript…but I can't read parameters sent.

I have this method responsible of connection:

protected String connect (String url , File file, String nomeFile, String ticket){
   
   HttpURLConnection connection = null;
   DataOutputStream dos = null;
   BufferedReader rd  = null;     
   StringBuilder sb = null;             
   URL serverAddress = null;
   
   int bytesRead, bytesAvailable, bufferSize;
   byte[] buffer;


   String lineEnd = "\r\n";
   String twoHyphens = "–-";
   String boundary = "*****";
   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");
         //Content-Type della form
         connection.setRequestProperty("Content-Type", "multipart/form-data;boundary=" + boundary);
         connection.connect();
         
         dos = new DataOutputStream( connection.getOutputStream() );

         dos.writeBytes(twoHyphens + boundary + lineEnd);
         //My data
         dos.writeBytes("Content-disposition: form-data; name=\"nome\"" + lineEnd);
         dos.writeBytes(lineEnd);
         dos.writeBytes(nomeFile);
         dos.writeBytes(lineEnd);
         dos.writeBytes(twoHyphens + boundary + lineEnd);
         
         dos.writeBytes("Content-disposition: form-data; name=\"step\"" + lineEnd);
         dos.writeBytes(lineEnd);
         dos.writeBytes("2");
         dos.writeBytes(lineEnd);
         dos.writeBytes(twoHyphens + boundary + lineEnd);
         
         dos.writeBytes("Content-Disposition: form-data; name=\"upload\";" + " filename=\"" + file +"\"" + lineEnd);
         dos.writeBytes(lineEnd);      
      
         FileInputStream fileInputStream = new FileInputStream(file);
      
         bytesAvailable = fileInputStream.available();
         bufferSize = bytesAvailable;
         buffer = new byte[bufferSize];
      
         bytesRead = fileInputStream.read(buffer, 0, bufferSize);
         while (bytesRead > 0) {

            dos.write(buffer, 0, bufferSize);
            bytesAvailable = fileInputStream.available();
            bufferSize =bytesAvailable;
            bytesRead = fileInputStream.read(buffer, 0, bufferSize);

         }   
         
         fileInputStream.close();
         
         dos.writeBytes(lineEnd);
         dos.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd);

         
         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'); 
            }
         
   
            }catch (IOException ioex) {
   
               System.out.println("From (ServerResponse): "+ioex);
   
            }
      
      }……

And in my backed java class I want to access req.getParameter("step"), but it is null.
Any suggestion?

nowhere
Champ in-the-making
Champ in-the-making
Here it's my HTTP POST request and response, sniffed:

POST /alfresco/service/ricerca/uploadDoc
HTTP/1.1
Connection: Keep-Alive
Content-Type: multipart/form-data;boundary=*****
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: 222

*****
Content-Disposition: form-data; name="nome"

documento
*****
Content-Disposition: form-data; name="step"

2
*****
Content-Disposition: form-data; name="upload"; filename="pwd.txt"

pwd2
pwd1
*****

HTTP/1.1 500 Internal Server Error
Server: Apache-Coyote/1.1
Cache-Control: no-cache
Pragma: no-cache
Content-Type: text/html;charset=UTF-8
Transfer-Encoding: chunked
Date: Fri, 19 Jun 2009 09:33:14 GMT
Connection: close

{
    "status" :
  {
    "code" : 500,
    "name" : "Internal Error",
    "description" : "An error inside the HTTP server which prevented it from fulfilling the request."
  }, 
 
  "message" : "05190042 Wrapped Exception (with status template): null", 
  "exception" : "org.alfresco.web.scripts.WebScriptException - 05190042 Wrapped Exception (with status template): null",
 
  "callstack" :
  [
       ""      ,"java.lang.NumberFormatException: null"
      ,"java.lang.Integer.parseInt(Integer.java:415)"
      ,"java.lang.Integer.<init>(Integer.java:620)"
      ,"com.verosa.ricerca.UploadDoc.executeImpl(UploadDoc.java:55)"
      ,"org.alfresco.web.scripts.DeclarativeWebScript.executeImpl(DeclarativeWebScript.java:223)"
      ,"org.alfresco.web.scripts.DeclarativeWebScript.execute(DeclarativeWebScript.java:70)"
      ,"org.alfresco.repo.web.scripts.RepositoryContainer$2.execute(RepositoryContainer.java:319)"
      ,"org.alfresco.repo.transaction.RetryingTransactionHelper.doInTransaction(RetryingTransactionHelper.java:326)"
      ,"org.alfresco.repo.transaction.RetryingTransactionHelper.doInTransaction(RetryingTransactionHelper.java:233)"
      ,"org.alfresco.repo.web.scripts.RepositoryContainer.transactionedExecute(RepositoryContainer.java:368)"
      ,"org.alfresco.repo.web.scripts.RepositoryContainer.transactionedExecuteAs(RepositoryContainer.java:390)"
      ,"org.alfresco.repo.web.scripts.RepositoryContainer.executeScript(RepositoryContainer.java:273)"
      ,"org.alfresco.web.scripts.AbstractRuntime.executeScript(AbstractRuntime.java:262)"
      ,"org.alfresco.web.scripts.AbstractRuntime.executeScript(AbstractRuntime.java:139)"
      ,"org.alfresco.web.scripts.servlet.WebScriptServlet.service(WebScriptServlet.java:122)"
      ,"javax.servlet.http.HttpServlet.service(HttpServlet.java:717)"
      ,"org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)"
      ,"org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)"
      ,"org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)"
      ,"org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)"
      ,"org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:128)"
      ,"org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)"
      ,"org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)"
      ,"org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:286)"
      ,"org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:845)"
      ,"org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:583)"
      ,"org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:447)"
      ,"java.lang.Thread.run(Thread.java:619)"
      ,"org.alfresco.web.scripts.WebScriptException: 05190042 Wrapped Exception (with status template): null"
      ,"org.alfresco.web.scripts.AbstractWebScript.createStatusException(AbstractWebScript.java:622)"

  ],
 
  "server" : "Alfresco Labs v3.2.0 (_Preview2_dev @build-number@) schema 2.011",
  "time" : "19-giu-2009 11.33.14"
}

Hoping It maybe usefull.

mrogers
Star Contributor
Star Contributor
"Step" is in your form data and is therefore "Content" and not a "Parameter".

nowhere
Champ in-the-making
Champ in-the-making
Thanks mrrogers, but I get empty with following code:

Content cont = req.getContent();
         try {
     System.out.println("Content: " + cont.getContent());
……


😞

Edit: data are on request as parameters (request.getParameter(name)). It was a mistake in POST request boundary.
Getting started

Tags


Find what you came for

We want to make your experience in Hyland Connect as valuable as possible, so we put together some helpful links.