Est-ce que j'envoie correctement un HTTPPost? Je vérifie mon encodedAuthString contre mon serveur web et peux vérifier que la chaîne est correcte. Je ne sais pas pourquoi je suis incapable de m'authentifier.Problème lors de l'envoi d'en-têtes HTTP en Java
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);
}
}
}
MISE À JOUR:
Quand je coder en dur la chaîne encodée, tout va bien.
Lorsque j'utilise l'encodeur base64-je obtenir les mêmes résultats que la chaîne codée, mais le serveur retourne un 401.
même lorsque j'essaie setHeader() cela ne fonctionne pas. –
@Sheehan Alam: Étant donné que j'ai des douzaines d'étudiants et plus d'une douzaine d'abonnés chaque mois en essayant l'échantillon auquel j'ai fait référence ci-dessus, je suis plutôt confiant que cela fonctionne. Vous aurez besoin d'un compte identi.ca pour l'essayer, y compris avoir cliqué sur le courriel de confirmation qu'ils ont envoyé. – CommonsWare
@CommonsWare: Merci. J'ai mis à jour mon code pour correspondre à votre exemple. J'utilise addHeader() et setEntity() mais toujours pas de chance. Votre code est presque identique au mien. –