Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

HTTP Upload file with Socket

Participant ,
Mar 09, 2016 Mar 09, 2016

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

            }

TOPICS
Scripting
5.6K
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Participant , Mar 15, 2016 Mar 15, 2016

I found it. I downloaded the source files of the spring framework and the apache commons file uploader und used the debugger to find out what happened on server side. The tomcat want's another "--" after the file data (and the closing boundary string) and I had a CRLF at this position. I just added the "--" at the end of the upload and it worked..

Translate
Enthusiast ,
Mar 10, 2016 Mar 10, 2016

Hi,

did you try passive mode ?

yourSocket.write('PASV\r\n')

response (yourSocket.readln();) should include '227'

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Mar 11, 2016 Mar 11, 2016

Hi,

Thanks, but this didn't work. Server gives me a "400 Bad Request" as response.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Mar 11, 2016 Mar 11, 2016

I am not sure if this will help, but Russ Ward has a bunch of free sample ExtendScript scripts on his website. I am pretty sure there is a socket example. They are oriented toward FrameMaker, but the socket part should be the same regardless of the target product. FrameMaker ExtendScript Samples - West Street Consulting

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
People's Champ ,
Mar 14, 2016 Mar 14, 2016

I would also recommend looking at getUrls script from Rorohiko : Rorohiko: GetURLs.jsx - access HTTP content from InDesign ExtendScript

Extendables maybe too ?

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Mar 15, 2016 Mar 15, 2016

Thanks, but the examples have only code with reading data via HTTP Get and that already works fine. The problem is in sending data with file attachements via POST.

I also tried the Extendables library and that doesn't work at all. Doesn't set the boundary and other header settings.

And I made a simple php script for the server side and the POST works with this PHP script. But the same POST doesn't work with our java server application with embedded Tomcat webserver...

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Mar 15, 2016 Mar 15, 2016

I found it. I downloaded the source files of the spring framework and the apache commons file uploader und used the debugger to find out what happened on server side. The tomcat want's another "--" after the file data (and the closing boundary string) and I had a CRLF at this position. I just added the "--" at the end of the upload and it worked..

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Mar 21, 2018 Mar 21, 2018

HI Klauss

I test your code for uploading by socket binary file... but not works....

Can you load an example of working code to understand where I'm wrong?


					
				
			
			
				
			
			
			
			
			
			
			
		
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Mar 21, 2018 Mar 21, 2018
LATEST

Thanks, but this is already solved.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines