Sending UTF-8 by POST does not work

0

Have the following connection class:

public class Conexao {

public static String postDados(String urlUsuario, String parametrosUsuario) {
    URL url;
    HttpURLConnection connection = null;

    try {

        url = new URL(urlUsuario);
        connection = (HttpURLConnection) url.openConnection();

        connection.setRequestMethod("POST");

        connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");

        connection.setRequestProperty("Content-Lenght", "" + Integer.toString(parametrosUsuario.getBytes().length));

        connection.setRequestProperty("Content-Language", "pt-BR");

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

        DataOutputStream dataOutputStream = new DataOutputStream(connection.getOutputStream());
        dataOutputStream.writeBytes(parametrosUsuario);
        dataOutputStream.flush();
        dataOutputStream.close();

        InputStream inputStream = connection.getInputStream();

        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"));
        String linha;
        StringBuffer resposta = new StringBuffer();

        while((linha = bufferedReader.readLine()) != null) {
         resposta.append(linha);
            resposta.append('\r');
        }

        bufferedReader.close();

        return resposta.toString();

    } catch (Exception erro) {

        return  null;
    } finally {

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

But when I try to send something by utf-8, for example, it does not stay in utf-8:

url = "url";
parametros = 

new cadastro.registro().execute(url);

To go, I have to send by get, in the url:

url = "url" + "?texto=" + "~~a@#";
parametros = "";

new cadastro.registro().execute(url);

But I need it to be for POST and not for GET, can anyone help me?

private class registro extends AsyncTask<String, Void, String> {
    @Override
    protected  String doInBackground(String... urls) {

        return Conexao.postDados(urls[0], parametros);

    }

    @Override
    protected void onPostExecute(String resultado) {
    }
    }
    
asked by anonymous 16.03.2018 / 14:24

3 answers

0

I was able to resolve:

I changed this:

 DataOutputStream dataOutputStream = new 
 DataOutputStream(connection.getOutputStream());
    dataOutputStream.writeBytes(parametrosUsuario);
    dataOutputStream.flush();
    dataOutputStream.close()

So:

OutputStreamWriter outPutStream = new 
OutputStreamWriter(connection.getOutputStream(), "utf-8");
        outPutStream.write(parametrosUsuario);
        outPutStream.flush();
        outPutStream.close();

Now it works perfectly.

    
16.03.2018 / 17:30
0

As you are not passing the type of Character Set , it will be the API that will define which charset to use, usually it is ISO-8859-1 , you can check with more details in RFC 8187 .

To specify the use of a specific charset, you need to pass this information to the HTTP protocol, using the Content-Type header as follows:

Content-Type: media-type;charset=charset-type

where:

  • media-type corresponds to the data format to be transmitted ( RFC 7231 )
  • charset indicates the character encoding scheme ( RFC 7231 )

For your case, you would need to change the line:

connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");

To:

connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded;charset=utf-8");
    
16.03.2018 / 15:02
0

If I understood correctly, is it to send the data by POST right? Try replacing the InputStream parameter with the following parameter:

InputStream inputStream = httpURLConnection.getInputStream();
    BufferedReader bufferedReader = new BufferedReader(new 
InputStreamReader(inputStream, "iso-8859-1"));

If you need to send a URL information in php that receives a parameter, you can do it as follows:

BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(outputStream, "UTF-8"));
            String post_data = URLEncoder.encode("parametro1", "UTF-8") + "=" + URLEncoder.encode(valor1, "UTF-8") + "&"
                    + URLEncoder.encode("parametro2", "UTF-8") + "=" + URLEncoder.encode(valor2, "UTF-8");
    
16.03.2018 / 15:07