0

我正在制作一个向 Ilias 服务器发出发布请求的应用程序,我收到响应并在 web 视图上打开它,我的问题是,如果我导航,当我点击某个链接时,它不会保存我的凭据并告诉我“你没有登录”,不要继续。

这是代码:

public class ilias extends Activity {

 WebView webView;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        webView = (WebView)findViewById(R.id.webview);

        BufferedReader bufferedReader = null;
        HttpClient httpClient = new DefaultHttpClient();
        HttpPost request = new HttpPost("http://www.ilias.de/docu/login.php?client_id=docu");
        List<NameValuePair> postParameters = new ArrayList<NameValuePair>();
        postParameters.add(new BasicNameValuePair("username", "stacked")); //this username 
        postParameters.add(new BasicNameValuePair("password", "overflow"));//works


  try {
   UrlEncodedFormEntity entity = new UrlEncodedFormEntity(postParameters);
         request.setEntity(entity);

         HttpResponse response= httpClient.execute(request);

   bufferedReader = new BufferedReader(
           new InputStreamReader(response.getEntity().getContent()));
   StringBuffer stringBuffer = new StringBuffer("");
   String line = "";
   String LineSeparator = System.getProperty("line.separator");
   while ((line = bufferedReader.readLine()) != null) {
    stringBuffer.append(line + LineSeparator); 
   }
   bufferedReader.close();

   Toast.makeText(ilias.this, 
     "Finished", 
     Toast.LENGTH_LONG).show();

   String webData = stringBuffer.toString();

   webView.loadData(webData,"text/html","UTF-8");
   webView.loadDataWithBaseURl("http://www.ilias.de/docu/",webData,"text/html","UTF-8","about:blank");

  } catch (ClientProtocolException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
   Toast.makeText(ilias.this, 
     e.toString(), 
     Toast.LENGTH_LONG).show();
  } catch (IOException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
   Toast.makeText(ilias.this, 
     e.toString(), 
     Toast.LENGTH_LONG).show();
  }finally{
   if (bufferedReader != null){
    try {
     bufferedReader.close();
    } catch (IOException e) {
     // TODO Auto-generated catch block
     e.printStackTrace();
    }
   }
  }

    }
}
4

2 回答 2

0

通过以这种方式进行 POST,您将失去存储 cookie 的能力。您还必须在WebView. 您可以直接使用来自 WebView 的 POST 和请求正文。看来您正在通过与 WebView 的post 方法兼容的 URL 编码实体传递用户名和密码。

尝试类似:

String query = "username=" + username + "&password=" + password";
webview.postUrl("http://www.ilias.de/docu/login.php?client_id=docu", query.getBytes());
于 2011-10-19T18:21:24.743 回答
0

如果您多次使用 httppost,您可以通过 httpclient 维护 cookie。如果您只是在网站中导航,那么该网站实际上应该存储您的信息,因为它由网站处理,否则将 httpclient 设为公共静态全局并在您的活动或其他活动中共享它。

于 2012-01-18T19:28:38.003 回答