1

Helo

I tryed to upload some data to my webserver. whit curl works fine

curl -s -uedoweb-admin:admin -F"data=@/home/raul/test/t1/6376990.pdf;type=application/pdf" -XPUT api.localhost/resource/frl:6376979/data

and here the trace http://pastebin.com/jJmungAy

here the new methode.

        URL myurl;
    HttpURLConnection conn;
    String port = "9000";

    String user = "edoweb-admin";
    String password = "admin";
    String encoding = Base64.encodeBase64String((user + ":" + password).getBytes());
    String boundary = "==" + System.currentTimeMillis() + "===";
    String crlf = "\r\n";
    String twoHyphens = "--";
    String attachmentName = "data";
    String attachmentFileName = "6376986.pdf";

    DataOutputStream request;

    try {
        myurl = new URL("http://localhost:9000/resource/frl:6376984/data");
        conn = (HttpURLConnection) myurl.openConnection();
        conn.setDoOutput(true);
        conn.setRequestMethod("PUT");
        conn.setRequestProperty("Content-Type", "multipart/form-data;boundary=" + boundary);
        conn.setRequestProperty("Accept", "*/*");

    } catch (Exception e) {
        throw new RuntimeException(e);
    }

    try {
        request = new DataOutputStream(conn.getOutputStream());

        request.writeBytes(twoHyphens + boundary + crlf);
        request.writeBytes("Content-Disposition: form-data; name=\"" + attachmentName + "\";filename=\""
                + attachmentFileName + "\"" + crlf);
        request.writeBytes("Content-Type: application/pdf");
        request.writeBytes(crlf);
        System.out.println(conn.getResponseCode());
    } catch (Exception e) {
        throw new RuntimeException(e);
    }

I found some issues in my code and as well on the webserver. I wrote a new methode and now the server response 400.

can anyone help me?

Raul Vasi
  • 88
  • 1
  • 11
  • Two things to improve diagnose. 1. Surround your test with try catch for Exception or THrowable to see if anything is going wrong. 2. Can you see on the server's logs you're using to test if the request actually makes it there? – Alfabravo Dec 09 '15 at 15:39
  • I surrounded multiple lines whit try-catch and its all green. The log shows nothing about my try. – Raul Vasi Dec 09 '15 at 15:55
  • Please edit your question with the code including the try-catch. The fact you're not reaching the server with the code means you're having a NPE or something – Alfabravo Dec 11 '15 at 15:53
  • Look, this question and its answer might help you, looks like that library has some odd behaviors. http://stackoverflow.com/questions/15678208/making-put-request-with-json-data-using-httpurlconnection-is-not-working – Alfabravo Dec 15 '15 at 18:43
  • This one as well http://stackoverflow.com/questions/1051004/how-to-send-put-delete-http-request-in-httpurlconnection – Alfabravo Dec 15 '15 at 18:44

1 Answers1

0

I found it out by myself.

here the code:

public void form_test() {
    try {
        url = new URL("http://localhost:9000/resource/frl:6376984/data");
        httpCon = (HttpURLConnection) url.openConnection();
        String userpass = user + ":" + password;
        basicAuth = "Basic " + new String(new Base64().encode(userpass.getBytes()));
        httpCon.setRequestProperty("Authorization", basicAuth);
        String fieldName = "data";
        File uploadFile = new File("/home/raul/test/frl%3A6376984/6376990.pdf");
        String boundary = "" + System.currentTimeMillis() + "";

        httpCon.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + boundary);
        httpCon.setRequestProperty("file", "6376986.pdf");

        httpCon.setUseCaches(false);
        httpCon.setDoOutput(true);
        httpCon.setDoInput(true);

        httpCon.setRequestMethod("PUT");

        OutputStream outputStream = null;
        try {
            outputStream = (httpCon.getOutputStream());
        } catch (IOException e) {
            e.printStackTrace();
        }
        PrintWriter writer = null;
        try {
            writer = new PrintWriter(new OutputStreamWriter(outputStream, "UTF-8"), true);
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        String LINE_FEED = "\r\n";
        String fileName = uploadFile.getName();
        writer.append("--" + boundary).append(LINE_FEED);
        writer.append("Content-Disposition: form-data; name=\"" + fieldName + "\"; filename=\"" + fileName + "\"")
                .append(LINE_FEED);
        writer.append("Content-Type: " + URLConnection.guessContentTypeFromName(fileName)).append(LINE_FEED);
        writer.append("Content-Transfer-Encoding: binary").append(LINE_FEED);
        System.out.println("Content-Transfer-Encoding: binary" + (LINE_FEED));
        writer.append(LINE_FEED);

        writer.flush();

        fileToOutputStream(uploadFile, outputStream);
        try {
            outputStream.flush();
        } catch (IOException e) {
            e.printStackTrace();
        }

        writer.append(LINE_FEED);
        writer.flush();
        writer.close();
        httpCon.disconnect();

        try {
            System.out.println(httpCon.getResponseCode());
            System.out.println(httpCon.getResponseMessage());
        } catch (IOException e) {
            e.printStackTrace();
        }

    } catch (Exception e) {
        e.printStackTrace();
    }
}

1st mistake was character "=" at the begin of the boundary String. Seems that my Webserver dont like so many equals in the boundary field, so I set just one here:

httpCon.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + boundary);

and removed those from the String boundary

String boundary = "" + System.currentTimeMillis() + "";

I also get some error message "Packet size out of Limit" in Wireshark and response Code 400 in Java when I try to upload the File but now with this method it works fine. Thank you guys for the support and the suggestions.

Raul Vasi
  • 88
  • 1
  • 11