当前位置:  开发笔记 > 编程语言 > 正文

以编程方式从WebView登录

如何解决《以编程方式从WebView登录》经验,为你挑选了0个好方法。

在我的Android应用程序中,我使用Web视图访问服务器提供的一些Web映射数据.服务器需要一些基于HTTP表单的身份验证才能访问这些数据.由于该网站没有移动版本,显示登录页面(或任何其他页面)看起来非常糟糕.不幸的是,该网站几乎无法触及,所以我想到了以下方法:

使用本机用户界面来收集用户名和密码

认为Http帖子将这些信息发送到服务器

收到响应后,获取服务器发送的cookie

将cookie设置为Web视图

尝试最终访问所需的数据

现在我只想尝试通过登录阶段.

这是一个可行的解决方案,还是完全错误的,我应该尝试别的吗?

为了完整起见,我发布以下代码

A.认证部分

 private String authenticate()  throws Exception
    {
        // Create a new HttpClient and Post Header
           HttpClient httpclient = new DefaultHttpClient();
           HttpPost httppost = new HttpPost("http://mySite/login_form");

           HttpResponse response = null;
           BufferedReader in = null;
           String resultContent = null;

           try
           {
               // Add data
               List nameValuePairs = new ArrayList();
               nameValuePairs.add(new BasicNameValuePair("came_from", ""));
               nameValuePairs.add(new BasicNameValuePair("form.submitted", "1"));
               nameValuePairs.add(new BasicNameValuePair("js_enabled", "0"));
               nameValuePairs.add(new BasicNameValuePair("cookies_enabled", ""));
               nameValuePairs.add(new BasicNameValuePair("login_name", ""));
               nameValuePairs.add(new BasicNameValuePair("pwd_empty", "0"));
               nameValuePairs.add(new BasicNameValuePair("name", "username"));
               nameValuePairs.add(new BasicNameValuePair("password", "password"));

               httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

                // Create a local instance of cookie store
                CookieStore cookieStore = new BasicCookieStore();
                // Create local HTTP context
                HttpContext localContext = new BasicHttpContext();
                // Bind custom cookie store to the local context
                localContext.setAttribute(ClientContext.COOKIE_STORE, cookieStore);
               // Execute HTTP Post Request
               response = httpclient.execute(httppost,localContext);

               in = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
               StringBuffer sb = new StringBuffer("");
               String line = "";
               String NL = System.getProperty("line.separator");
               while ((line = in.readLine()) != null)
               {
                 sb.append(line + NL);
               }
               in.close();
               resultContent = sb.toString();
               Log.i("mytag","result :"+resultContent);

               cookies = new java.util.ArrayList();
               cookies = cookieStore.getCookies();

           }
           catch (ClientProtocolException e)
           {
               Log.i("mytag","Client protocol exception");
           }
           catch (IOException e)
           {
               Log.i("mytag","IOException");
           }
           catch(Exception e)
           {
               Log.i("mytag","Exception");
               Log.i("mytag",e.toString());
           }


        return resultContent;

    }

B.设置cookie并加载所需的页面

private void init()
{
            CookieSyncManager.createInstance(this);
            CookieManager cookieMan= CookieManager.getInstance();
            cookieMan.setAcceptCookie(true);
            cookies = StartupActivity.listAfter;

            if(cookies != null)
            {
                for (int i = 0; i

加载该页面的结果是显示login_page表单

推荐阅读
手机用户2402851335
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有