2 Replies Latest reply on Dec 13, 2011 1:04 AM by Corwin86

    Http post: answer always with value 1

    Corwin86

      Hi all,

       

      I have this code below. I always gain the value “1″ in return; no file, or other values…

      This mean that the value sent by "POST" il always the value "1".

      Where is the error?

      Thanks very much!

       

      var myFile = new File("C:/APPS/Test/temp.jpg");
      var myLine = "";
      var autorisation = "";

          // count length

          boundaryContent = "--1234\n";
      boundaryContent += 'Content-Disposition:form-data; name="upload"; filename="'+myFile.name+'"' + "\n";
      boundaryContent += "Content-Type: " + "image/jpeg" + "\n";
      boundaryContent += "\n";
      myFile.open("r");
      myLine = myFile.read();
      boundaryContent += myLine;
      myFile.close();
      boundaryContent += "\n";
      boundaryContent += "--1234--" + "\n";

          alert("Calculated File Size: " + boundaryContent.length);

          // end count

      conn1 = new Socket;

          if (conn1.open ("127.0.0.1:8888", "BINARY")) {

              conn1.timeout=9000;

              conn1.writeln("POST /Enterprise/config/plugins/NoWoodwing/servlet_temp.php HTTP/1.1");
      conn1.writeln("Host: 127.0.0.1:8888");
      conn1.writeln("Keep-Alive: 300");
      conn1.writeln("Connection: keep-alive");
      conn1.writeln("Content-Length: "+ boundaryContent.length);

        conn1.writeln("Expect: 100-continue");
      conn1.writeln("Content-Type: multipart/form-data; boundary=1234");
      conn1.writeln("");
      var boundaryContent1 = conn1.writeln("--1234");
      boundaryContent1 +=  conn1.writeln('Content-Disposition:form-data; name="upload"; filename="'+myFile.name+'"');
      boundaryContent1 +=  conn1.writeln("Content-Type: " + "image/jpeg");
      boundaryContent1 +=  conn1.writeln("");
      autorisation = conn1.readln();
      alert("Autorizzazione: " + autorisation);
      if (autorisation == "HTTP/1.1 100 Continue") {
      if (myFile.open("r")) {
      myLine = myFile.read();
      alert("Nuova linea: " + myLine);
      boundaryContent1 +=  conn1.write(myLine);
      myFile.close();
      }
      }
      boundaryContent1 += conn1.writeln("");
      boundaryContent1 +=  conn1.write("--1234--");

              alert("File Size Post Elaboration: " + boundaryContent1);

              /*
      var reply1 = '';
      while(!conn1.eof)  {
      reply1 += conn1.readln();
      };
      // alert("Return:" + reply1);
      */

              conn1.close();

              } else { alert("Errore: " + conn1.error); }

        • 1. Re: Http post: answer always with value 1
          John Hawkinson Level 5

          Does it work if you execute the file upload with some other tool, such as a browser?

           

           

          conn1 = new Socket; 
          if (conn1.open ("127.0.0.1:8888", "BINARY")) {
              conn1.timeout=9000;
              conn1.writeln("POST /Enterprise/config/plugins/NoWoodwing/servlet_temp.php HTTP/1.1");
          

           

          Well, we're certainly not going to be able to test that. Can you find an example of a publicly available server that accepts uploads and test against that?

          • 2. Re: Http post: answer always with value 1
            Corwin86 Level 1

            Hi John Hawkinson,

             

            sorry for the delay of the response...

             

            I tested the code as you suggested, and I found out where the fault lay: must be very careful to correctly calculate the "Content-Length", or the result returned by the post will be completely wrong.

             

            Thanks again.

             

            Best regards.