0

I've 'fixed' this but I don't understand why my fix works and that bothers me. The code below is supposed to an HTTP POST to a remote server. I have two lines commented out. With the two lines commented out, it appears to write to the HTTP connection it has opened, but nothing is received on the remote server. There are no errors and no exceptions. With the two lines uncommented, requesting a response HTTP code, the data appears to be actually sent. Why? I wrote this originally without the two lines, assuming that a flush and a close of the connection would behave in the same manner as flushing and closing a file connection: what is in the buffer gets written. But this appears not to be the case. Is there a better way to flush and send the POST data than what I am doing?

    private void post()
            throws IOException {
        System.out.println("Posting xml to url " + postUrl);
        byte[] postData = htmlOutput.getBytes();
        URL post = new URL(postUrl);

        HttpURLConnection conn = (HttpURLConnection) post.openConnection();
        conn.setDoOutput(true);
        conn.setInstanceFollowRedirects(false);
        conn.setRequestMethod("POST");
        conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
        conn.setRequestProperty("charset", "utf-8");
        conn.setRequestProperty("Content-Length", Integer.toString(postData.length));
        conn.setUseCaches(false);
        DataOutputStream wr = new DataOutputStream(conn.getOutputStream());
        wr.write(postData);
        wr.flush();
        wr.close();

        conn.disconnect();
//        int responseCode = conn.getResponseCode();
//        System.out.println("POST Response Code from server was " + responseCode);

        System.out.println("Finished posting xml to url " + postUrl);
    }
Warren
  • 179
  • 2
  • 7
  • *Curious:* Why wrap in a `DataOutputStream` when all you're doing is call `write(byte[])`? – Andreas Apr 11 '20 at 16:08
  • *FYI:* `charset` is not an HTTP header value, it is an attribute of `Content-Type`, i.e. it should be `Content-Type", "application/x-www-form-urlencoded; charset=utf-8"`, and then you need to ensure that the bytes actually *are* UTF-8, by specifying that on the `htmlOutput.getBytes()` call as `getBytes(StandardCharsets.UTF_8)`. – Andreas Apr 11 '20 at 16:10
  • I don't think I need UTF-8. The original code came from an example I found on the web, and when I worked on this I switched over to using OutputStream, which seems more appropriate. Do you know why getting a reference to the InputStream is needed before the java library will do the post? As I say in my description, I had assumed this would work like writing to a file. – Warren Apr 12 '20 at 09:00
  • Did you read the answer in the "This question already has an answer here:" link at the top? Basically, until you ask for some information from the response, the request is still being built and buffered in memory, so it isn't sent until then. – Andreas Apr 12 '20 at 10:19
  • Yes, I did read that. I understand that this is part of what needs to be done to get the java code to do a POST. I do not understand exactly WHY it is done this way, and I do not understand why there is no warning or exception if you don't DO it this way, but I accept it. Lesson learned. I do want to thank you for staying on this question even though you closed it. Your commentary helped. – Warren Apr 12 '20 at 18:37
  • If you want, point me in the direction of how I can handle responses with invalid Content-Type (or maybe none). I may have to handle a response with a Content-Type, according to Java of 'unknown/unknown'. I know there are bytes to be read, but I cannot get past the exception that the Java library throws in this case. FireFox can display a URL of this type, so I know it can be done. – Warren Apr 12 '20 at 18:38
  • That's an entirely different question. The question posed *here* (why you need to call `getResponseCode()`) was already answered in another question, which is why your question has been closed as a duplicate. If you have another question, e.g. about handling the response, you need to create a new question and supply all relevant information, e.g. an example of the response, and what it is about handling that response that is an issue for you. – Andreas Apr 12 '20 at 21:40

0 Answers0