2012-02-28 96 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

相关问题