'How to make in java an http post returning binary data using plain Sockets

Because some restrictions, I have to use plain Java sockets to download a file published in a http web site. This is how i am reading the response:

String serverIp = "192....";
int serverPort = 3000;
String url = "/path/to/file";

Socket socket = new Socket(serverIp, serverPort);

BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));

String postContent = "content";

writer.write("POST " + url + " HTTP/1.0\r\n");
writer.write("Content-length: " + postContent.length() + "\r\n");
writer.write("\r\n");
writer.write(postContent);
writer.flush();

BufferedReader reader = new BufferedReader(new InputStreamReader(socket.getInputStream()));

String line = null;

while ((line = reader.readLine()) != null) {
    if (!line.trim().equals("")) {
        //Process header
    } else {
        break;
    }
}

int intChar = -1;
ByteArrayOutputStream out = new ByteArrayOutputStream();

while ((intChar = reader.read()) >= 0) {
    out.write(intChar);
}
byte[] byteArray = out.toByteArray();

File outFile = new File("myfile.zip");
FileOutputStream fileOutputStream = new FileOutputStream(outFile);
fileOutputStream.write(byteArray);
fileOutputStream.close();


Every thing works fine, but the file myfile.zip is saved inconsistent. If I use unzip to uncompress the file, I get the error:

Archive:  myfile.zip
error [myfile.zip]:  missing 55053 bytes in zipfile
  (attempting to process anyway)
error [myfile.zip]:  start of central directory not found;
  zipfile corrupt.
  (please check that you have transferred or created the zipfile in the
  appropriate BINARY mode and that you have compiled UnZip properly)

When I use curl to do the http post, myfile.zip download consistent and I can open it.

Any light?



Solution 1:[1]

Thanks guys. I used the suggestion of President James K. Polk to write the following solution:


String serverIp = "192....";
int serverPort = 3000;
String url = "/path/to/file";

Socket socket = new Socket(serverIp, serverPort);

BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));

String postContent = "content";

writer.write("POST " + url + " HTTP/1.0\r\n");
writer.write("Content-length: " + postContent.length() + "\r\n");
writer.write("\r\n");
writer.write(postContent);
writer.flush();

DataInputStream reader = new DataInputStream(socket.getInputStream());
int c = -1;
StringBuilder header = new StringBuilder();

while ((c = reader.read()) >= 0) {
    header.append((char) c);
    if (header.length() > 4 && header.substring(header.length() - 4).equals("\r\n\r\n")) {
        break;
    }
}

File outFile = new File("myfile.zip");
FileOutputStream fileOutputStream = new FileOutputStream(outFile);
while ((c = reader.read()) >= 0) {
    fileOutputStream.write(c);
}

reader.close();
fileOutputStream.close();

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 RDV