2012-02-28 97 views
2

我能夠從Android程序向遠程服務器發送請求,但似乎請求沒有參數。我如何附加參數?如何將請求參數附加到當前Android HttpUrlConnection調用

這裏是我當前的代碼:

@Override 
    protected String doInBackground(String... theParams) 
    { 

     String myUrl = theParams[0]; 
     final String myEmail = theParams[1]; 
     final String myPassword = theParams[2]; 

     Authenticator.setDefault(new Authenticator() 
     { 
      protected PasswordAuthentication getPasswordAuthentication() 
      { 
       return new PasswordAuthentication(myEmail, myPassword.toCharArray()); 
      } 
     });   

     String response = null; 

     try 
     { 
      final URL url = new URL(myUrl); 

      final HttpURLConnection conn = (HttpURLConnection) url.openConnection(); 
      conn.setRequestProperty("login", myEmail); 
      conn.setRequestProperty("password", myPassword); 

      conn.setUseCaches(false); 

      final InputStream is = conn.getInputStream(); 
      final byte[] buffer = new byte[8196]; 
      int readCount; 
      final StringBuilder builder = new StringBuilder(); 
      while ((readCount = is.read(buffer)) > -1) 
      { 
       builder.append(new String(buffer, 0, readCount)); 
      } 

       response = builder.toString();  
      Log.d("After call, response: " , " " + response); 
     } 
     catch (Exception e) 
     { 

     } 

     return response; 
    } 

所以,現在我不知道如何進行身份驗證的密碼/登入得到重視的請求,並會發送到服務器。任何想法我怎麼能做到這一點?

謝謝!

回答

3

你只需做到這一點

final URL url = new URL(myUrl+"?login="+myEmail+"&password="+myPassword); 

而且你不需要setRequestProperty線。這些實際上是設置你的Http Request的屬性而不是查詢參數。

+0

@Nikil但這不是一個安全缺陷?不應該隱藏密碼嗎?我沒有在我的情況下使用https。謝謝! – Genadinik 2012-02-28 20:59:50

+0

這是一個不同的問題。除非您有加密/解密邏輯或通過SSL進行通信,否則傳遞的憑據將始終以純文本格式顯示。 – Nikhil 2012-02-29 04:48:34

+0

@NikhilPatil,你好我想談談你有關在Android中添加參數到POST請求的問題 – 2015-04-14 11:35:23

相關問題