6 Replies Latest reply on Mar 15, 2016 5:46 AM by KlausFriese

    HTTP Upload file with Socket

    KlausFriese

      Hi,

       

      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';

                  f.open("r");

                  var fContent = f.read();

                  f.close();

       

                  if( conn.open( "127.0.0.1:8081" , "BINARY" ) ) {

                      alert( "connected" );

                      conn.timeout=20000;

                      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: 127.0.0.1:8081\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 );

                      log.close();

                     

                      conn.write( cs );

                      reply = conn.read(999999);

                      conn.close();

                      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 );

                  }