Results 1 to 5 of 5
  1. #1
    guerlando is offline Member
    Join Date
    Mar 2016
    Posts
    11
    Rep Power
    0

    Default java https request gives strange characters like "Núm = N??m"

    Hi, I have the following sample of code:

    Java Code:
    HttpsURLConnection con = (HttpsURLConnection) url.openConnection();
      con.setInstanceFollowRedirects(false);
      con.setRequestProperty("Content-length", String.valueOf(postQuery.length()));
      con.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
      con.setRequestProperty("User-Agent", "Mozilla/5.0 (compatible; MSIE 5.0;Windows98;DigExt)");
      con.setDoInput(true);
    I also pass some POST data with con.setDoOutput(true) and con.setRequestMethod("POST") but I don't think this is relevant. Then, I read the data like this:

    Java Code:
    String charset = "ISO-8859-1";
      BufferedReader buff = new BufferedReader(
       new InputStreamReader(con.getInputStream(), charset));
      String response = "";
      String line;
      while ((line = buff.readLine()) != null) {
       this.response += line + "\n";
      }

    When I see the results, words like "Número" appear as "N??mero". Basically anything with a strange character appears like this, even though I used a charset that supports them. I want to understand the root of this problem. When java requests a page, is it delivered in which format? Like, it's all bytes, the java client receives bytes, but are those 8 bits bytes or 16 bits? How is the mapping between the bytes and words? Then, what exactly BufferedReader receives from con.getInputStream()? I need to understand it so I can correct what's happening. Thank you so much.

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    13,541
    Rep Power
    26

    Default Re: java https request gives strange characters like "Núm = N??m"

    Are you sure the input stream charset is ISO-8859-1?

    How are you checking the value of 'response"?
    What tools are you using?
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  3. #3
    guerlando is offline Member
    Join Date
    Mar 2016
    Posts
    11
    Rep Power
    0

    Default Re: java https request gives strange characters like "Núm = N??m"

    I tried UTF-8 which is the page encoding, same problem. I'm checking by System.out.println(). I'm just using command line and CodeAnywhere.com

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    13,541
    Rep Power
    26

    Default Re: java https request gives strange characters like "Núm = N??m"

    The command line has its own character set, so is unlikely to be able to display that character.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  5. #5
    guerlando is offline Member
    Join Date
    Mar 2016
    Posts
    11
    Rep Power
    0

    Default Re: java https request gives strange characters like "Núm = N??m"

    I tried in my own machine, where the character encoding of the terminal is set to UTF-8, and now I get Números instead of N??meros. Does that ring a bell?

Similar Threads

  1. Replies: 0
    Last Post: 12-07-2012, 08:29 AM
  2. Strange "Cannot find symbol" error...
    By JimmyD in forum Advanced Java
    Replies: 7
    Last Post: 04-19-2012, 09:37 PM
  3. Replies: 2
    Last Post: 10-03-2011, 06:22 PM
  4. Replies: 1
    Last Post: 10-20-2008, 07:35 AM
  5. Strange error message "Source not found"
    By ppayal in forum Eclipse
    Replies: 0
    Last Post: 11-25-2007, 06:19 PM

Tags for this Thread

Posting Permissions

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