Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/http/4.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
如何在不同的活动android中维护http post后的Cookie?_Android_Http_Webview_Http Post_Session Cookies - Fatal编程技术网

如何在不同的活动android中维护http post后的Cookie?

如何在不同的活动android中维护http post后的Cookie?,android,http,webview,http-post,session-cookies,Android,Http,Webview,Http Post,Session Cookies,我在网上搜索了三天多,但没有找到任何解决办法。我正在进行http post登录,然后打开一个webview,它是我正在登录的webserver的链接。问题是webview没有显示经过身份验证的页面,请帮助我。 代码如下: ` 公共类MainActivity扩展活动{ 私有字符串sessionokie; 私人厨师经理; 专用静态最终整数超时\u MS=3000; 私有网络视图; 专用静态最终字符串redirURL=”http://slateisb.nu.edu.pk/portal/relogin"

我在网上搜索了三天多,但没有找到任何解决办法。我正在进行http post登录,然后打开一个webview,它是我正在登录的webserver的链接。问题是webview没有显示经过身份验证的页面,请帮助我。 代码如下:

`

公共类MainActivity扩展活动{
私有字符串sessionokie;
私人厨师经理;
专用静态最终整数超时\u MS=3000;
私有网络视图;
专用静态最终字符串redirURL=”http://slateisb.nu.edu.pk/portal/relogin";
@凌驾
创建公共空间(捆绑冰柱){
超级冰柱;
setContentView(R.layout.activity_main);
//------------------饼干-----------------------//
CookieSyncManager.createInstance(此);
CookieManager CookieManager=CookieManager.getInstance();
Date dateObj=新日期();
dateObj.setTime(dateObj.getTime()+2*7*24*60*60*1000);
字符串sA=“acc=”+0;
String sL=“lgn=”;
SimpleDataFormat后格式化程序=新的SimpleDataFormat(“EEE MMM dd yyyy HH:mm:ss zzz”);
String oD=postformator.format(dateObj);
字符串cookieString=“logondata=“+sA+”&“+sL+”;expires=“+oD;
cookieManager.setCookie(redirl,cookieString);
cookieManager.getCookie(redirl);
CookieSyncManager.getInstance().sync();
//------------------网络视图-----------------------//
CookieSyncManager.createInstance(此);
CookieSyncManager.getInstance().startSync();
mWebView=(WebView)findViewById(R.id.webView1);
WebSettings WebSettings=mWebView.getSettings();
webSettings.setSavePassword(true);
webSettings.setSaveFormData(真);
setJavaScriptEnabled(true);
webSettings.setSupportZoom(假);
setWebViewClient(新的WebViewClient()){
公共布尔值shouldOverrideUrlLoading(WebView视图,字符串url){
//请在此处输入您的处理代码,哪个url是请求的url
//您可能需要打开该url,而不是重定向:
view.loadUrl(url);
return false;//则默认操作不处理
}
});
//------------------------------HTTP 4.0重定向--------------------------//
HttpClient HttpClient=新的DefaultHttpClient();
HttpConnectionParams.setConnectionTimeout(httpClient.getParams(),超时);
HttpConnectionParams.setSoTimeout(httpClient.getParams(),超时);
HttpPost HttpPost=新的HttpPost(redirURL);
List nameValuePairs=新的ArrayList();
添加(新的BasicNameValuePair(“curl”、“varl”);
添加(新的BasicNameValuePair(“标志”,“0”));
添加(新的BasicNameValuePair(“forcedownlevel”、“0”);
添加(新的BasicNameValuePair(“formdir”,“9”));
添加(新的BasicNameValuePair(“eid”、“i120515”);
添加(新的BasicNameValuePair(“pw”、“password123”);
添加(新的BasicNameValuePair(“受信任的”、“1”));
HttpResponse end=null;
字符串endResult=null;
试一试{
setEntity(新的UrlEncodedFormEntity(nameValuePairs));
HttpResponse response=httpClient.execute(httpPost);
结束=响应;
}捕获(例外e){
//TODO自动生成的捕捉块
e、 printStackTrace();
} 
BasicResponseHandler myHandler=新BasicResponseHandler();
试一试{
endResult=myHandler.HandlerResponse(结束);
}捕获(HttpResponseException e){
//TODO自动生成的捕捉块
e、 printStackTrace();
}捕获(IOE异常){
//TODO自动生成的捕捉块
e、 printStackTrace();
}
//加载数据(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);
}
@凌驾
公共布尔onCreateOptions菜单(菜单){
//为菜单充气;这会将项目添加到操作栏(如果存在)。
getMenuInflater().充气(R.menu.main,menu);
返回true;
}
}
`我终于找到了解决这个问题的办法。 如果我使用异步任务调用http请求并将cookie存储到输出文件中。然后,我可以轻松地在我想要的任何活动中使用cookies。
也可以在不同的应用中使用这些cookies。

对不起,我不明白。你能说得更具体些吗?这意味着将cookie存储到输出文件中是什么意思?
    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;
}