Skip to content Skip to sidebar Skip to footer

Httpurlconnection.getresponsecode() Freezes Execution/doesn't Time Out

I'm writing an Android app that connects to a cPanel server (Apache 2.2.22) page which is password protected. When the authentication credentials are correct, I have no problem con

Solution 1:

It seems to be an issue with using Authenticator in POST connections. It's quite old so I don't know if it still exists.

I would try two things:

  • Add a log line in the getPasswordAuthentication of the Authenticator to see if it's effectively called. If nothing is printed, you should check that you add the default Authenticator before it's called. You say you do it in the onCreate(), so it should be fine but it's good to be sure.
  • Avoid using the Authenticator (at least for testing purposes) and send the auth info directly in the HTTP Request. I usually do it this way:

    Stringauth= user + ":" + pass;
    conn = (HttpURLConnection) url.openConnection();
    conn.setRequestProperty("Authorization", 
                   "Basic " + Base64.encode(auth.getBytes()));
    // Set other parameters and read the result...

Solution 2:

The problem was that the 401 Unauthorized status is sent when the Authorization header is missing and when the credentials contained within the header are incorrect. Therefore, my app was constantly sending the same request over and over to no avail. I have therefore found a workaround to the problem by adding a counter into my CustomAuthenticator:

publicclassCustomAuthenticatorextendsAuthenticator {

    publicstaticintRETRIES=3;

    int mRetriesLeft;
    Context mContext;

    publicCustomAuthenticator(Context context){
        super();
        mRetriesLeft = RETRIES;
        mContext = context;
    }

    @Overrideprotected PasswordAuthentication getPasswordAuthentication() {

        Log.i(getClass().getName(), "getPasswordAuthentication() - mCounter: " + mRetriesLeft);

        if(mRetriesLeft > 0){       

            SharedPreferencessharedPreferences= PreferenceManager.getDefaultSharedPreferences(mContext);
            Stringusername= sharedPreferences.getString(SettingsActivity.KEY_USERNAME_PREFERENCE, null);
            Stringpassword= sharedPreferences.getString(SettingsActivity.KEY_PASSWORD_PREFERENCE, null);

            mRetriesLeft--;
            returnnewPasswordAuthentication(username, password.toCharArray());

        } else {
            Log.w(getClass().getName(), "No more retries. Returning null");
            mRetriesLeft = RETRIES;
            returnnull;
        }
    }

    publicvoidreset(){
        mRetriesLeft = RETRIES;
    }
}

I should say however that I do not like this solution and therefore, have not accepted it. You have to remember to reset the counter whenever you make a new request (I do it in AsyncTask.onPreExecute()), or else every third request will fail. Also, I'm sure there must be a native way to do this, although after scouring the documentation I can't find it. I would still be very grateful if anyone can point it out to me.

Solution 3:

I don't know if I am right or not but my solution has worked for me for a whole day without a glitch.

Try doing this

byte[] buf = newbyte[4096];
Inputstream is;
do
{
    http conn code etc;
    is=conn.getInputStream();

    if(is.read(buf)==0)             
    {
        flag=1;
    }

    //u can either is.close(); or leave as is//codeint serverResponseCode = connection.getResponseCode();
    String serverResponseMessage = connection.getResponseMessage();     
    conn.disconnect();

} while(flag==1);

Post a Comment for "Httpurlconnection.getresponsecode() Freezes Execution/doesn't Time Out"