Problem with POST Urlconnection

Asked

Viewed 96 times

0

When sending parameter through the POST to a URL, I am redirected to another URL, it is redirecting me to the right URL, but it returns me:

<script LANGUAGE=JavaScript>alert('Seu tempo expirou. Por favor entre novamente');location.href='logon.asp';</script>

Code of my Activity:

public class HttpPostAsyncTask extends AsyncTask<String,Void,String> {
    @Override
    protected String doInBackground(String... param) {

        content = new StringBuilder();
        String response= "";

        try {

            URL link = new URL(url+path);
            HttpURLConnection.setFollowRedirects(true);
            HttpURLConnection conn = (HttpURLConnection) link.openConnection();
            conn.setInstanceFollowRedirects(true);
            conn.setDoOutput(true);
            conn.setRequestMethod("POST");
            conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");

            String charset = "UTF-8";
            String s = "txtnumero_matricula=" + URLEncoder.encode(login, charset);
            s += "&txtsenha_tac=" + URLEncoder.encode(senha, charset);
            s += "&cmdEnviar=" + URLEncoder.encode(param, charset);

            conn.setFixedLengthStreamingMode(s.getBytes().length);
            PrintWriter out = new PrintWriter(conn.getOutputStream());
            out.print(s);
            out.close();

            System.out.print(conn.getURL());

            conn.connect();

            Scanner inStream = new Scanner(conn.getInputStream());

            while(inStream.hasNextLine())
                response += (inStream.nextLine());

                System.out.println(conn.getURL()+"  "+response);

        } catch (ProtocolException e1) {
            e1.printStackTrace();
        } catch (UnsupportedEncodingException e1) {
            e1.printStackTrace();
        } catch (IOException e1) {
            e1.printStackTrace();
        }
        return response.toString();
    }

    @Override
    protected void onPostExecute(String html) {
        super.onPostExecute(html);

    }
}

I did more tests and any parameter I send being right or wrong, it returns me the same SCRIPT.

  • Wouldn’t your server-side problem be? Technically the android application is working right?

  • So at first I thought it was, but I tested it later with the apache httpclient library I pass the same parameters, and it returns me the full page as I want. So I think that something is missing or Urlconnection does not solve my problem.

  • Have a look at this link here: http://answall.com/questions/124457/alterar-o-c%C3%B3digo-httpparams-para-httpurlconnection works 100%

  • Thanks, but I still have the same problem, it does not return me the full page with the POST, only the SCRIPT saying that my time has expired... but if I try to give a GET on another page that does not have to login it returns me the full page... Will we use cookies? or add more to the Header?

No answers

Browser other questions tagged

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