up vote 1 down vote favorite
1
share [fb]

Am I correctly sending an HTTPPost? I am checking my encodedAuthString against my webserver and can verify the string is correct. Not sure why I am unable to authenticate.

public JSONObject connect(String url) {
            HttpClient httpclient = new DefaultHttpClient();

            // Prepare a request object
            HttpPost httppost = new HttpPost(url);

            String authString = usernameEditText.getText().toString() + ":" + passwordEditText.getText().toString();
            String encodedAuthString = Base64.encodeToString(authString.getBytes(),Base64.DEFAULT);
            String finalAuthString = "Basic " + encodedAuthString;

            // Execute the request
            HttpResponse response;
            try {  
                httppost.addHeader("Authorization", finalAuthString);

                response = httpclient.execute(httppost);
                // Examine the response status
                Log.i("Praeda", response.getStatusLine().toString());

                // Get hold of the response entity
                HttpEntity entity = response.getEntity();

                if (entity != null) {

                    // A Simple JSON Response Read
                    InputStream instream = entity.getContent();
                    String result = convertStreamToString(instream);

                    // A Simple JSONObject Creation
                    JSONObject json = new JSONObject(result);

                    // Closing the input stream will trigger connection release
                    instream.close();

                    return json;
                }

            } catch (ClientProtocolException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            } catch (JSONException e) {
                e.printStackTrace();
            }

            return null;
        }

        @Override
        protected JSONObject doInBackground(String... urls) {
            return connect(urls[0]);
        }

        @Override
        protected void onPostExecute(JSONObject json) {
            String authorized = "200";
            String unauthorized = "401";
            String notfound = "404";
            String status = new String();

            try {
                // Get the messages array
                JSONObject response = json.getJSONObject("response");
                status = response.getString("status");

                if(status.equals(authorized))
                    Toast.makeText(getApplicationContext(), "Authorized",Toast.LENGTH_SHORT).show();
                else if (status.equals(unauthorized))
                    Toast.makeText(getApplicationContext(), "Unauthorized",Toast.LENGTH_SHORT).show();
                else if(status.equals(notfound))
                    Toast.makeText(getApplicationContext(), "Not found",Toast.LENGTH_SHORT).show();

            } catch (JSONException e) {
                System.out.println(e);
            } catch (NullPointerException e) {
                System.out.println(e);
            }
        }
    }

UPDATE:

When I hard-code the encoded string, everything is fine.

When I use the Base64 encoder I get the same results as the encoded string, but the server returns a 401.

link|improve this question

feedback

1 Answer

up vote 2 down vote accepted

You are not setting any headers in this code. Use setHeader() to set headers. This too is covered in the documentation for HttpClient, as was the information from your previous question.

Here is a sample project that sets the Authorization header. Note that it uses a separate Base64 encoder, since the built-in one only showed up with Android 2.2, IIRC.

link|improve this answer
even when I try setHeader() it does not work. – Sheehan Alam Nov 23 '10 at 1:24
@Sheehan Alam: Considering that I have dozens of students and more dozens of subscribers every month trying the sample I linked to above, I feel rather confident that it works. You will need an identi.ca account to try it, including having clicked through on the email confirmation they send out. – CommonsWare Nov 23 '10 at 1:25
@CommonsWare: Thanks. I have updated my code to match your example. I am using addHeader() and setEntity() but still no luck. Your code is almost identical to mine. – Sheehan Alam Nov 23 '10 at 1:36
@CommonsWare: Updated. I hard-coded the authorization header and everything works. When I don't hard code, i get the same string, but get a 401 back from the server. – Sheehan Alam Nov 23 '10 at 1:52
@Sheehan Alam: If you have not done so already, switch to the Base64 encoder included in my project -- I have not tried the one in Android 2.2. Also, if this is your server, log what you are getting for the authentication string on the server side. – CommonsWare Nov 23 '10 at 1:52
show 2 more comments
feedback

Your Answer

 
or
required, but never shown

Not the answer you're looking for? Browse other questions tagged or ask your own question.