2010-11-23 82 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()來設置標題。 HttpClient的documentation也包含這一點,您的previous question的信息也是如此。

Here is a sample project設置授權標頭。請注意,它使用單獨的Base64編碼器,因爲內置的編碼器僅在Android 2.2,IIRC中出現。

+0

即使我嘗試setHeader()它不起作用。 – 2010-11-23 01:24:23

0

我在字符串中看到額外\n,所以不是使用默認選項,我用no_wrap選項是這樣的:

Base64.encodeToString(authordata.getBytes(),Base64.NO_WRAP);

它爲我工作。