0

In my java application I used a Httpsurlconnection to post some string data to the server. When I test this code on android, it works perfectly. However, in a java application it does not work. Client java application is as follows:

public static void main(String[] args) {

    disableSslVerification();
    new HttpsClient().testIt();
}

private void testIt() {

    String https_url = "https://XXX.XX.XXX.XXX:XXXX/XXXXX/TestServlet";
    URL url;
    try {

        url = new URL(https_url);
        HttpsURLConnection con = (HttpsURLConnection) url.openConnection();
        print_content(con, "test");

    } catch (MalformedURLException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

}

private void print_content(HttpsURLConnection connection, String data) {
    if (connection != null) {

        try {
            connection.setConnectTimeout(6000);
            connection.setRequestMethod("POST");
            connection.setRequestProperty("Content-Type",
                    "application/x-www-form-urlencoded");
            Charset cSet = Charset.forName("UTF-8");
            byte bytes[] = data.getBytes(cSet);
            connection.setRequestProperty("Content-Length", ""
                    + Integer.toString(bytes.length));
            connection.setRequestProperty("Content-Language", "tr");
            connection.setRequestProperty("Accept-Charset", "UTF-8");

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

            DataOutputStream wr = new DataOutputStream(
                    connection.getOutputStream());

            wr.write(bytes);
            wr.flush();
            wr.close();

            InputStream is = connection.getInputStream();
            BufferedReader rd = new BufferedReader(new InputStreamReader(is, cSet));
            String line;
            StringBuilder response = new StringBuilder();
            while ((line = rd.readLine()) != null) {
                response.append(line);
                response.append(System.getProperty("line.separator"));
            }
            rd.close();
            System.out.println(response.toString());

        } catch (Exception e) {
            System.out.println(e.getMessage());

        } finally {
            if (connection != null) {
                connection.disconnect();
            }
        }

    }

}

And the servlet is as follows:

@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    response.setContentType("text/html;charset=UTF-8");
    PrintWriter out = response.getWriter();
    String s = getHTML(request);
    try {
        out.print("received data:");
        out.print(s);
    } finally {            
        out.close();
    }
}

private String getHTML(HttpServletRequest request) throws IOException {
    int n = request.getContentLength();
    if (n < 1) {
        return "";
    }

    byte bytes[] = new byte[n];
    request.getInputStream().read(bytes);
    return new String(bytes, "UTF-8");
}

When I run this application, servlet's response is:

received data:t☐☐☐

Always only the first character is correctly send to the servlet. The same code works perfect on android. Can anyone help me please? Thanks...

user2972185
  • 231
  • 3
  • 12

2 Answers2

0
request.getInputStream().read(bytes);

You might need to do this read in a loop. At the very least, check how many bytes have been read. The array appears to be empty except for the first char.

Reads some number of bytes from the input stream and stores them into the buffer array b. The number of bytes actually read is returned as an integer. This method blocks until input data is available, end of file is detected, or an exception is thrown.

mttdbrd
  • 1,791
  • 12
  • 17
  • Thank you for your answer but I had already tried that but nothing changed. Moreover, if something was wrong with reading bytes, why did the same servlet work for android app? – user2972185 Mar 29 '14 at 23:40
0

I can't see an obvious problem with your code that would cause this.

Can anyone help me please?

I suggest that you take a methodical approach to investigating the problem. Use a packet sniffer to check what is actually being sent over the wire. Check that the actual headers in the request and response are correct. Check that the request and response bodies are really properly encoded UTF-8 ...

What you find in your investigation / evidence gathering will help you figure out where the problem (or problems) are occurring ... and that will allow you to home in on the part(s) of your code that is/are responsible.

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
  • I'd suggest trying to read a single byte at a time in a loop as well instead of request.getInputStream().read(bytes);. At the very least, OP has to check the return code of that method call. It'll tell how many bytes have been read. – mttdbrd Mar 29 '14 at 01:30
  • @mttdbrd - You are assuming that the problem is in the way the bytes are being read. That is a premature assumption. He needs to gather some evidence first. – Stephen C Mar 29 '14 at 01:37
  • Thank you for your answer. I am using httpsurlconnection. Can I sniff encrypted data? Because httpurlconnection also works. The only problem occurs when using httpsurlconnection in java app. – user2972185 Mar 29 '14 at 23:46
  • Switch to HTTP temporarily. – Stephen C Mar 30 '14 at 00:07
  • But no problem occurs when using httpurlconnection (HTTP) in Java Application or using httpsurlconnection in android app. This problem arises in only one case: using httpsurlconnection (HTTPS) in java app. – user2972185 Mar 31 '14 at 01:43
  • @user2972185 - That suggests that the problem is at the SSL connection level. Have you configured the server-side properly? Is the client-side attempting to connect to the right server-side port? I've seen strange things happen when requests from an HTTPS client end up going to an HTTP stack on the server side, or vice-versa. – Stephen C Mar 31 '14 at 03:15