2010-11-23 14 views
2

正しくHTTPPostを送信していますか?私は私のencodedAuthStringを私のウェブサーバに対してチェックしており、文字列が正しいことを確認することができます。なぜ私が認証できないのか分かりません。JavaでHTTPヘッダーを送信する際の問題

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:

私はハードコードエンコードされた文字列、すべてが正常です。

私はBase64でエンコーダを使用する場合、私はエンコードされた文字列と同じ結果を得るが、サーバはあなたがこのコード内の任意のヘッダを設定していない401

答えて

4

を返します。ヘッダーを設定するにはsetHeader()を使用してください。これは、previous questionの情報と同様に、HttpClientのdocumentationにも記載されています。

Here is a sample project Authorizationヘッダーを設定します。組み込みのものはAndroid 2.2、IIRCでしか見えなかったので、別のBase64エンコーダを使用していることに注意してください。

+0

setHeader()を試しても動作しません。 –

+0

@Sheehan Alam:毎月、数十人の学生と数十人の加入者がサンプルを試していることを考えると、私はそれがうまくいくと確信しています。あなたが送信する確認メールをクリックしたことを含め、identi.caアカウントが必要です。 – CommonsWare

+0

@CommonsWare:ありがとう。あなたの例に合わせてコードを更新しました。私はaddHeader()とsetEntity()を使用していますが、まだ運がありません。あなたのコードは私のものとほとんど同じです。 –

0

私は、文字列内の余分な\nを見て、その代わりに、デフォルトのオプションを使用するので、私はこのようなno_wrapオプション使用:

Base64.encodeToString(authordata.getBytes()、Base64.NO_WRAPを)。

と私のために働いた。