Results 1 to 5 of 5
Like Tree1Likes
  • 1 Post By gimbal2

Thread: sendResponseHeaders & ERR_CONTENT_LENGTH_MISMATCH

  1. #1
    Rexilion is offline Member
    Join Date
    Mar 2015
    Posts
    5
    Rep Power
    0

    Default sendResponseHeaders & ERR_CONTENT_LENGTH_MISMATCH

    This is my second day of Java programming. I have some experience in VBA & Shell.

    I'm currently stuck on serving the static image below (which I have downloaded to a buffer).

    I guess, that my mistake is in the following line:

    t.sendResponseHeaders(200, lengtht);

    Google chrome says: *Errorcode: ERR_CONTENT_LENGTH_MISMATCH*

    However, I'm constructing the var lengtht by looping over a buffer and storing the read values.

    Java Code:
    import java.net.*;
    import java.io.*;
    
    import com.sun.net.httpserver.*;
    
    public class SimpleHttpServer {
    
      public static void main(String[] args) throws Exception {
        HttpServer server = HttpServer.create(new InetSocketAddress(8000), 0);
        server.createContext("/test", new MyHandler());
        server.setExecutor(null); // creates a default executor
        server.start();
      }
    
      static class MyHandler implements HttpHandler {
        public void handle(HttpExchange t) throws IOException {
        	URL url = new URL("https://shop.plantconnect.nl/Afbeelding.ashx/1324353172012");
    		InputStream is = url.openStream();
    
    		byte[] b = new byte[8192];
    		int length;
    		int lengtht = 0;
    
          //String response = t.getRequestURI().toString();
          OutputStream os = t.getResponseBody();
          while ((length = is.read(b)) != -1) {
          	  lengtht = lengtht + length;
          	  System.out.println(length);
          }
          is.close();
          System.out.println(lengtht);
    
          Headers responseHeaders = t.getResponseHeaders();
          responseHeaders.set("Content-Type","image/jpeg");
    
          t.sendResponseHeaders(200, lengtht);
          os.write(b, 0, lengtht);
          os.close();
        }
      }
    }

  2. #2
    Rexilion is offline Member
    Join Date
    Mar 2015
    Posts
    5
    Rep Power
    0

    Default Re: sendResponseHeaders & ERR_CONTENT_LENGTH_MISMATCH

    I think I'm seeing my rookie mistake. I'm only writing the last contents of the buffer...

  3. #3
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    5,114
    Rep Power
    12

    Default Re: sendResponseHeaders & ERR_CONTENT_LENGTH_MISMATCH

    It helps to just say out in english what your code does.

    Java Code:
    while ((length = is.read(b)) != -1) {
              lengtht = lengtht + length;
              System.out.println(length);
          }
    English:

    "keep reading 8k of bytes into the exact same buffer until there is nothing more to read."

    When you read that English version, do you perhaps spot the thinking mistake?
    Rexilion likes this.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  4. #4
    Rexilion is offline Member
    Join Date
    Mar 2015
    Posts
    5
    Rep Power
    0

    Default Re: sendResponseHeaders & ERR_CONTENT_LENGTH_MISMATCH

    Hello gimbal,

    Yes, that was my mistake. I know have a working example.

    For the purpose of education and polishing, I have another thread about this piece of code here.

  5. #5
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    5,114
    Rep Power
    12

    Default Re: sendResponseHeaders & ERR_CONTENT_LENGTH_MISMATCH

    Okay, but you could have just continued here :)
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •