Copy link to clipboard
Copied
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 );
}
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..
Copy link to clipboard
Copied
Hi,
did you try passive mode ?
yourSocket.write('PASV\r\n')
response (yourSocket.readln();) should include '227'
Copy link to clipboard
Copied
Hi,
Thanks, but this didn't work. Server gives me a "400 Bad Request" as response.
Copy link to clipboard
Copied
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
Copy link to clipboard
Copied
I would also recommend looking at getUrls script from Rorohiko : Rorohiko: GetURLs.jsx - access HTTP content from InDesign ExtendScript
Extendables maybe too ?
Copy link to clipboard
Copied
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...
Copy link to clipboard
Copied
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..
Copy link to clipboard
Copied
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?
Copy link to clipboard
Copied
Thanks, but this is already solved.