UTF-8 sending by POST does not work

Asked

Viewed 526 times

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();
        }
    }
}
}

However when I try to send something by utf-8 like this, for example, it does not get in utf-8:

url = "url";
parametros = 

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

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

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

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

But I need it to be by POST and not by GET, someone can 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) {
    }
    }
  • Try to pass the charset on Content-Type: connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded;charset=utf-8");

  • @Henry I will test here now and I will return

  • @Henry man, it seems that now the paramedics are not going

  • @Henrique pera, it was yes, I q typed a wrong business here kkkk, but not in utf-8, in mysql the record where it had special character got ? like, j?

3 answers

0

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

To specify the use of a specific charset, it is necessary to pass this information to the HTTP protocol, using the header Content-Type 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)

In your case, it would be necessary to change the line:

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

for:

connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded;charset=utf-8");
  • As I said before, it didn’t work, the only way was to put utf8_encode($_POST['name']); in php

0

If I understand correctly, are you sending the data through the right POST? Try to replace 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 information to php URL that receives a parameter you can do 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");
  • I’ll take a look at this is already in return

  • If necessary, I can help assemble every request, because in your question, you use some methods that I do not use.

  • each, I did here, but it didn’t work, the characters remain strange

  • Ah, and forgive me, I was having lunch here kkkk

  • Are you using this URL ? url = "url" + "? text=" + "~~a@#";

  • this "url" in case it would be my url q eu dx so n to show, but the sending occurs, I put an echo in php to see what return, and this is not in utf-8, but I managed to solve here, I will post the answer below

Show 1 more comment

0


I managed to solve:

I switched it on:

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

That’s why:

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

Now it works perfectly.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.