When creating the connection activity in the connection.getRequestMethod ("POST") field; does not accept the Post method.
I'm new to the area, could you help me?
public static String postdados(String urlusuario, String parametrousuario) {
URL url;
HttpURLConnection connection = null;
try{
url = new URL (urlusuario);
connection = (HttpURLConnection) url.openConnection();
connection.getRequestMethod("POST");
connection.setRequestProperty( "content-type", "application/x-www-form-urlencoded" );
connection.setRequestProperty( "content-lenght", "" + Integer.toString( parametrousuario.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(parametrousuario);
dataOutputStream.flush();
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();
}
}
}
}