manvendra dubey
manvendra dubey

Reputation: 31

sending username and password as param in http request


I got one of the sample codes online that uses Restful api for simple login and registration. I have already got the external jar file and compiled it but one of the method is not getting resolved.
The "invokeWS" method is giving error ,though the RequestParams worked fine, please suggest. This is the link that I used to get the jar file--
http://loopj.com/android-async-http/

I am putting the code here

package com.example.dell.restful_demo;

import android.app.DownloadManager;
import android.app.ProgressDialog;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.loopj.android.http.RequestParams;
import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.AsyncHttpResponseHandler;


public class LoginActivity extends AppCompatActivity {

// Progress Dialog Object
ProgressDialog prgDialog;
// Error Msg TextView Object
TextView errorMsg;
// Email Edit View Object
EditText emailET;
// Passwprd Edit View Object
EditText pwdET;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_login);

    // Find Error Msg Text View control by ID
    errorMsg = (TextView) findViewById(R.id.login_error);
    // Find Email Edit View control by ID
    emailET = (EditText) findViewById(R.id.loginEmail);
    // Find Password Edit View control by ID
    pwdET = (EditText) findViewById(R.id.loginPassword);
    // Instantiate Progress Dialog object
    prgDialog = new ProgressDialog(this);
    // Set Progress Dialog Text
    prgDialog.setMessage("Please wait...");
    // Set Cancelable as False
    prgDialog.setCancelable(false);

}


public void loginUser(View view) {
    // Get Email Edit View Value
    String email = emailET.getText().toString();
    // Get Password Edit View Value
    String password = pwdET.getText().toString();
    // Instantiate Http Request Param Object
    RequestParams params = new RequestParams();
    // When Email Edit View and Password Edit View have values other than Null
    if (Utility.isNotNull(email) && Utility.isNotNull(password)) {
        // When Email entered is Valid
        if (Utility.validate(email)) {
            // Put Http parameter username with value of Email Edit View control
            params.put("username", email);
            // Put Http parameter password with value of Password Edit Value control
            params.put("password", password);
            // Invoke RESTful Web Service with Http parameters
            invokeWS(params);
        }
        // When Email is invalid
        else {
            Toast.makeText(getApplicationContext(), "Please enter valid email", Toast.LENGTH_LONG).show();
        }
    }
    // When any of the Edit View control left blank
    else {
        Toast.makeText(getApplicationContext(), "Please fill the form, don't leave any field blank", Toast.LENGTH_LONG).show();
    }

}

}

Upvotes: 1

Views: 1008

Answers (1)

Leonardo Oliveira
Leonardo Oliveira

Reputation: 36

If you are using Basic Authentication, you must send both username and password through the authorization header, not the body parameters.

Here it is an example:

GET /securefiles/ HTTP/1.1
Host: www.httpwatch.com
Authorization: Basic aHR0cHdhdGNoOmY=

https://www.httpwatch.com/httpgallery/authentication/

The credentials are encoded in Base64, you can see an explanation here:

https://security.stackexchange.com/questions/29916/why-does-http-basic-authentication-encode-the-username-and-password-with-base64

Upvotes: 1

Related Questions