8 Replies Latest reply on Mar 21, 2018 1:17 AM by Klaus Friese

    HTTP Upload file with Socket

    KlausFriese Level 1



      I am currently working on scripts that are using the Socket for HTTP-communication with our server, I found many answers to my question by searching in the forum but now I'm stucked with an HTTP upload.


      I'm creating a preview of the current pages and I like to upload this preview to the server. On the server a java application is running and I have a java test application to upload files - I know that the upload is working and the problem is in the script.


      I'm reading the file into a String and I'm creating a Socket connection to the server. I'm creating another string with the Content I like to upload ( the file and additional informations ) and I'm sending the HTTP POST to the server. And I always get an "stream ended unexpectedly" error from the server.


      So I think, the content length is the problem. Server is expecting a number of bytes and I don't send the expected bytes. How do I calculate the content lenght?


      This is my code:


                                conn = new Socket;

                  reply = "";


                  var f = File ( "c:\\tmp\\jdExport.jpg");

                  f.encoding = 'BINARY';


                  var fContent = f.read();



                  if( conn.open( "" , "BINARY" ) ) {

                      alert( "connected" );


                      content = "--XLuvdG51D_BRiiAda_0y79ImMN_ddtKYzeFFLlu8\n"

                                      + "Content-Disposition: form-data; name=\"file\"; filename=\"jdExport.jpg\"\n"

                                      + "Content-Type: application/octet-stream\n"

                                      + "\n"

                                      + fContent;


                      cs = "POST /dp/uploadpreview?sessionid=xxxx HTTP/1.1\n"

                                      + "Content-Length: " + content.length + "\n"

                                      + "Content-Type: multipart/form-data; boundary=XLuvdG51D_BRiiAda_0y79ImMN_ddtKYzeFFLlu8\n"

                                      + "Host:\n"

                                      + "Connection: Keep-Alive\n"

                                      + "User-Agent: Apache-HttpClient/4.3.1 (java 1.5)\n"

                                      +  "Authorization: Basic TXlTUUxcYWRtaW46\n"

                                      + "Accept-Encoding: gzip,deflate\n"

                                      + "\n"

                                      + content;


                      var log = File( "c:\\tmp\\log.txt" );

                      log.open( "w" );

                      log.write( cs );



                      conn.write( cs );

                      reply = conn.read(999999);


                      alert( reply );

                      if( reply.indexOf( "200 OK" ) > 0 ) {

                      } else {

                          throw new Error( "createLayout.error: Errormessage from server " + dataplanServer + ":" + getResponseError( reply ) );


                  } else {

                      throw new Error( "createLayout.error: Can't connect to " + dataplanServer );