POST Request with token

1
  • Start an HTTP request via the POST method for the authentication URL, with the following parameters:

    • login
    • password

    response = link "MY_TOKEN"

  • The response to the request made in 1 will be answered by another URL. This URL represents the location of the system instance to which the user belongs, so a new request must be made to the URL you entered.

  • Does anyone know how to do step 2? I'm using Android Asynchronous Http Client

    An example in Linux bash / shell script

        
    asked by anonymous 30.08.2014 / 04:11

    1 answer

    5

    See if it's more or less this (if the token has to go inside the post):

      HttpClient httpclient = new DefaultHttpClient();
      HttpPost httppost = new HttpPost("http://api.dominio.com:8025/name/login");
    
      try {
         List nameValuePairs = new ArrayList();
    
         // Aqui setamos o token:
         nameValuePairs.add(new BasicNameValuePair("token", "ab124b3a1c2f"));
         // repita a linha de cima quantas vezes necessário,
         // com outras variaveis e parâmetros desejados.
    
         httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
         HttpResponse response = httpclient.execute(httppost);
      } catch (ClientProtocolException e) {
         // trate dos erros aqui
      } catch (IOException e) {
         // e aqui
      }
    
      

    Note that I did not post the code using async , I put the answer just as an initial reference. Depending on the case, async ends up upsetting the flow when it comes to interdependent requests, as you have to manage the events in your code.

    Edit : You may need to use it this way, by putting the entire URL here:

     HttpPost httppost = new HttpPost( response );
    

    and remove the POST token, leaving only the other variables needed, if any:

     nameValuePairs.add(new BasicNameValuePair("variavel1", "valor1..."));
     nameValuePairs.add(new BasicNameValuePair("variavel2", "valor2..."));
    
        
    30.08.2014 / 04:51