0

我在互联网上搜索了三天多,但没有找到任何解决方案。我正在为登录做http post,然后我打开一个webview,它是我正在登录的网络服务器的链接。问题是 webview 没有将页面显示为经过身份验证,请帮助我。这是代码:

`

    public class MainActivity extends Activity {
        private String sessionCookie;
        private CookieManager cookieManager;
         private static final int TIMEOUT_MS = 3000;
            private WebView mWebView;
            private static final String redirURL = "http://slateisb.nu.edu.pk/portal/relogin";


    @Override
    public void onCreate(Bundle icicle) {
        super.onCreate(icicle);
        setContentView(R.layout.activity_main);

        //------------------ COOKIES -----------------------//
        CookieSyncManager.createInstance(this); 
        CookieManager cookieManager = CookieManager.getInstance(); 
        Date dateObj = new Date();

        dateObj.setTime(dateObj.getTime() + 2 * 7 * 24 * 60 * 60 * 1000);
        String sA = "acc=" + 0;
        String sL = "lgn=";
        SimpleDateFormat postFormater = new SimpleDateFormat("EEE MMM dd yyyy HH:mm:ss zzz"); 
        String oD = postFormater.format(dateObj);   
        String cookieString = "logondata=" + sA + "&" + sL + "; expires="+ oD; 
        cookieManager.setCookie(redirURL, cookieString);
        cookieManager.getCookie(redirURL);
        CookieSyncManager.getInstance().sync(); 



        //------------------ WEBVIEW -----------------------//
        CookieSyncManager.createInstance(this);
        CookieSyncManager.getInstance().startSync();
        mWebView = (WebView) findViewById(R.id.webView1);

        WebSettings webSettings = mWebView.getSettings();
        webSettings.setSavePassword(true);
        webSettings.setSaveFormData(true);
        webSettings.setJavaScriptEnabled(true);
        webSettings.setSupportZoom(false);

        mWebView.setWebViewClient(new WebViewClient() {
            public boolean shouldOverrideUrlLoading(WebView view, String url){
                // do your handling codes here, which url is the requested url
                // probably you need to open that url rather than redirect:
                view.loadUrl(url);
                return false; // then it is not handled by default action
           }

        });

        //------------------------------ HTTP 4.0 REDIRECT --------------------------//

        HttpClient httpClient = new DefaultHttpClient();
        HttpConnectionParams.setConnectionTimeout(httpClient.getParams(), TIMEOUT_MS);
        HttpConnectionParams.setSoTimeout(httpClient.getParams(), TIMEOUT_MS);
        HttpPost httpPost = new HttpPost(redirURL);  
        List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();  
        nameValuePairs.add(new BasicNameValuePair("curl", "varl"));  
        nameValuePairs.add(new BasicNameValuePair("flags", "0")); 
        nameValuePairs.add(new BasicNameValuePair("forcedownlevel", "0"));    
        nameValuePairs.add(new BasicNameValuePair("formdir", "9"));
        nameValuePairs.add(new BasicNameValuePair("eid", "i120515"));  
        nameValuePairs.add(new BasicNameValuePair("pw", "password123"));  
        nameValuePairs.add(new BasicNameValuePair("trusted", "1"));
        HttpResponse end = null;
        String endResult = null;

        try {
            httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
            HttpResponse response = httpClient.execute(httpPost);

            end = response;
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } 

        BasicResponseHandler myHandler = new BasicResponseHandler();

        try {
            endResult = myHandler.handleResponse(end);
        } catch (HttpResponseException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

       //mWebView.loadData(endResult, "text/html", "utf-8");
        mWebView.loadUrl("http://slateisb.nu.edu.pk");
        //mWebView.loadDataWithBaseURL("http://slateisb.nu.edu.pk/portal/pda", endResult, "text/html", "utf-8", null);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

} `

4

1 回答 1

0

我终于找到了解决方案。如果我使用异步任务调用 http 请求并将 cookie 存储到输出文件。然后,我可以轻松地在任意数量的活动中使用 cookie。并且还可以在不同的应用程序中使用这些 cookie。

于 2013-07-24T19:59:04.223 回答