Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/379.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 从WebView以编程方式登录_Java_Android_Apache - Fatal编程技术网

Java 从WebView以编程方式登录

Java 从WebView以编程方式登录,java,android,apache,Java,Android,Apache,在我的Android应用程序中,我使用web视图访问服务器提供的一些web映射数据。服务器需要一些基于HTTP表单的身份验证来允许访问这些数据。由于该站点没有移动版本,因此显示登录页面(或任何其他页面)看起来非常糟糕。不幸的是,我很难接触到该网站,因此我想到了以下方法: 使用本机用户界面收集用户名和密码 认为Http post会将这些信息发送到服务器 收到响应后,获取服务器发送的cookies 将Cookie设置为web视图中的 尝试最终访问所需的数据 现在我只是想通过登录阶段 这是一个可行

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

  • 使用本机用户界面收集用户名和密码
  • 认为Http post会将这些信息发送到服务器
  • 收到响应后,获取服务器发送的cookies
  • 将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<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
               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;

    }
private String authenticate()引发异常
{
//创建一个新的HttpClient和Post头
HttpClient HttpClient=新的DefaultHttpClient();
HttpPost HttpPost=新的HttpPost(“http://mySite/login_form");
HttpResponse响应=null;
BufferedReader in=null;
字符串resultContent=null;
尝试
{
//添加数据
List nameValuePairs=新的ArrayList();
添加(新的BasicNameValuePair(“来自”、”);
添加(新的BasicNameValuePair(“提交的表格”,“1”));
添加(新的BasicNameValuePair(“已启用js_”,“0”);
添加(新的BasicNameValuePair(“cookies_enabled”);
添加(新的BasicNameValuePair(“登录名”);
添加(新的BasicNameValuePair(“pwd_empty”,“0”);
添加(新的BasicNameValuePair(“名称”、“用户名”);
添加(新的BasicNameValuePair(“密码”、“密码”));
setEntity(新的UrlEncodedFormEntity(nameValuePairs));
//创建cookie存储的本地实例
CookieStore CookieStore=新的BasicCookieStore();
//创建本地HTTP上下文
HttpContext localContext=新的BasicHttpContext();
//将自定义cookie存储绑定到本地上下文
setAttribute(ClientContext.COOKIE_存储,cookieStore);
//执行HTTP Post请求
response=httpclient.execute(httppost,localContext);
in=新的BufferedReader(新的InputStreamReader(response.getEntity().getContent());
StringBuffer sb=新的StringBuffer(“”);
字符串行=”;
字符串NL=System.getProperty(“line.separator”);
而((line=in.readLine())!=null)
{
sb.追加(行+NL);
}
in.close();
resultContent=sb.toString();
Log.i(“mytag”,“结果:+resultContent”);
cookies=new java.util.ArrayList();
cookies=cookieStore.getCookies();
}
捕获(客户端协议例外e)
{
Log.i(“mytag”,“客户端协议异常”);
}
捕获(IOE异常)
{
Log.i(“mytag”、“IOException”);
}
捕获(例外e)
{
Log.i(“mytag”、“例外”);
Log.i(“mytag”,例如toString());
}
返回结果内容;
}
B.设置cookies并加载所需页面

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

            if(cookies != null)
            {
                for (int i = 0; i<cookies.size(); i++)
                {
                    Cookie cookie = cookies.get(i);
                    cookieMan.setCookie("cookie.getDomain()",cookie.getValue());
                }
            }
            CookieSyncManager.getInstance().sync();

            webView = (WebView)findViewById(R.id.web_view);
            webView.getSettings().setJavaScriptEnabled(true);
            webView.getSettings().setBuiltInZoomControls(true);
            webView.setWebViewClient(new HelloWebViewClient());

}

     protected void onResume()
     {
            super.onResume();    

            // test if the we logged in
            webView.loadUrl("mySite/myDesiredFeature");

     }
private void init()
{
CookieSyncManager.createInstance(此);
CookieManager cookieMan=CookieManager.getInstance();
cookieMan.setAcceptCookie(真);
cookies=StartupActivity.listAfter;
如果(cookies!=null)
{
对于(int i=0;i1),首先尝试发出HttpGet请求,获取cookies,然后执行HttpPost。我认为这种方式不应该手动添加cookies。
使用一个HttpClient来执行此操作

2) 而不是

           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();
使用
EntityUtils.toString(response.getEntity())。

我可能也会这么做