【问题标题】:Want to pass the cookies to the webview to load a webpage想要将 cookie 传递给 webview 以加载网页
【发布时间】:2015-11-05 13:54:15
【问题描述】:

我使用 jsoup 成功登录了一个网站,我从 Map 中的 Jsoup cookie 功能获得了 cookie,但我对某些事情感到震惊。成功登录后,我想在 webview 中打开该网站的网页,但网页重定向到登录页面,因为 cookie 没有成功传递到 webview,我不知道为什么。该页面希望成功登录的 cookie 加载该页面

这是登录页面的网址。

http://111.68.99.8/StudentProfile/

成功登录后我要加载的页面

http://111.68.99.8/StudentProfile/PersonalInfo.aspx

这里是 MainActivity.java

package com.example.ebad.bu;

import android.app.ProgressDialog;
import android.content.Intent;
import android.os.AsyncTask;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.View;
import android.webkit.WebView;
import android.widget.Button;
import android.widget.TextView;

import org.jsoup.Connection;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;


public class MainActivity extends ActionBarActivity {


    String url = "http://111.68.99.8/StudentProfile/";
    Map<String, String> logingcookies;
    HashMap<String,String> hashMap;

    ProgressDialog progressDialog,progressDialoge;

    Button login, personal;
    TextView Enrollement, password, E;
    String enrol = "";
    String pass = "";


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        login = (Button) findViewById(R.id.login_button);
        personal = (Button) findViewById(R.id.pers);

        login.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Enrollement = (TextView) findViewById(R.id.Enrollment);
                enrol = Enrollement.getText().toString();

                password = (TextView) findViewById(R.id.password);
                pass = password.getText().toString();

                new Title().execute();


            }
        });



    }

    private class Title extends AsyncTask<Void, Void, Void> {
        String title;
        String father;
        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            progressDialog = new ProgressDialog(MainActivity.this);
            progressDialog.setTitle("Title");
            progressDialog.setMessage("Loading");
            progressDialog.setIndeterminate(false);
            progressDialog.show();
        }

        @Override
        protected Void doInBackground(Void... params) {

            try {
                Connection.Response loginForm = Jsoup.connect(url).method(Connection.Method.GET).timeout(1000).execute();
                Document doc = loginForm.parse();
                String viewstate = doc.select("input[name=__VIEWSTATE]").attr("value");
                String stategenerator = doc.select("input[name=__VIEWSTATEGENERATOR]").attr("value");
                String Eventvalidation = doc.select("input[name=__EVENTVALIDATION]").attr("value");


                doc = Jsoup.connect(url)
                        .data("__LASTFOCUS", "")
                        .data("__EVENTTARGET", "")
                        .data("__EVENTARGUMENT", "")
                        .data("__VIEWSTATE", viewstate)
                        .data("__VIEWSTATEGENERATOR", stategenerator)
                        .data("__EVENTVALIDATION", Eventvalidation)
                        .data("ctl00$Body$ENROLLMENTTextBox$tb", enrol)
                        .data("ctl00$Body$ENROLLMENTTextBox$cv_vce_ClientState", "")
                        .data("ctl00$Body$PasswordTextBox$tb", pass)
                        .data("ctl00$Body$PasswordTextBox$cv_vce_ClientState", "")
                        .data("ctl00$Body$LoginButton", "Login")
                        .cookies(loginForm.cookies())
                        .post();

                logingcookies = loginForm.cookies();


                title = doc.select("div[id=ctl00_Accounts]").html();


            } catch (IOException e) {
                e.printStackTrace();
            }
            return null;
        }

        @Override
        protected void onPostExecute(Void aVoid) {




            /*Intent showadditionmenu = new Intent(MainActivity.this,Per.class);
            //showadditionmenu.putStringArrayListExtra("logingcookies", (ArrayList<String>) logingcookies);
            showadditionmenu.putExtra("logingcookies", String.valueOf(logingcookies));
            MainActivity.this.startActivity(showadditionmenu);*/

          /*  Intent i = new Intent(MainActivity.this,Per.class);
            i.putExtra("hashMap",hashMap );
            startActivity(i);
*/
            hashMap = new HashMap<String,String>(logingcookies);
            Intent i = new Intent(MainActivity.this,wee.class);
            i.putExtra("hashMap",hashMap );
            startActivity(i);

          /*  E = (TextView) findViewById(R.id.message);
            E.setText(title);
            progressDialog.dismiss();*/
        }


    }

}

我已将保存的 cookie 传递给在 webivew 中加载网页的 Activity。这是另一个 Activity;

Wee.java

package com.example.ebad.bu;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.webkit.WebView;
import android.webkit.WebViewClient;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by Ebad on 11/8/2015.
 */
public class wee extends Activity {

    String url= "http://111.68.99.8/StudentProfile/PersonalInfo.aspx";
    HashMap<String ,String> hashMap;
    Map<String,String> map;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.layout);
        Intent intent = getIntent();
        hashMap = (HashMap<String,String>) intent.getSerializableExtra("hashMap");

        WebView webView = (WebView) findViewById(R.id.webviewe);
        webView.setWebViewClient(new WebViewClient());
        webView.loadUrl(url,hashMap);

    }

}

有什么方法可以让我在不重新登录 webview 的情况下查看该网页?

【问题讨论】:

    标签: javascript android cookies webview jsoup


    【解决方案1】:

    尚未对其进行测试,但使用 CookieManager 应该可以。

    用法应该是这样的:

    CookieSyncManager.createInstance(this);
    CookieManager cookieManager = CookieManager.getInstance();
    cookieManager.removeAllCookie(); //optional
    cookieManager.setAcceptCookie(true);
    

    然后使用 setCookie 方法循环访问您的 cookie。

    注意:如果使用 lollipop 及更高版本,您还应该使用 setAcceptThirdPartyCookies

    祝你好运。

    【讨论】:

    • CookieSyncManager 现已弃用
    • 您应该阅读文档:“此类在 API 级别 21 中已弃用。WebView 现在会根据需要自动同步 cookie。您不再需要创建或使用 CookieSyncManager。要手动强制同步,您可以使用 CookieManager 方法 flush(),它是 sync() 的同步替代。"首先,目前没有很多应用程序具有最低 SDK 21。其次,这仅在您使用 WebView 获取 cookie 时才相关,而您没有这样做。所以如果你想为你的网络视图设置 cookie,我想不出更好的方法。
    猜你喜欢
    • 2016-04-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-02-25
    • 2015-08-07
    • 2012-12-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多