通过应用程序通过HttpPost登录到网站 [英] Logging in via HttpPost to a website via an application

查看:295
本文介绍了通过应用程序通过HttpPost登录到网站的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好Stackoverflowers!

Hello Stackoverflowers!

我写了一个相对简单的应用程序,包含一个登录文本字段,密码文本字段以及登录按钮的。

I have written a relatively simple application that consists of a login text field, a password text field and a login button.

我的目标是,当用户输入登录信息,触动登录按钮,应用程序将使用户登录到我指定的网站,并在不同的意图或网页视图中打开它。我目前的实施打开了一个新的活动具有的WebView并传递登录信息。我的code是新的活动如下:

My goal is that when the user enters the login information and touches the login button, the application will log the user into the website I have specified and open it up in a different intent or WebView. My current implementation opens a new activity with a WebView and passes in the login information. My code is as follows for the new activity:

setContentView(R.layout.web);


    try {
        //add the users login information(username/pw) to a List
        List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
        nameValuePairs.add(new BasicNameValuePair("email", "random@gmail.com"));
        nameValuePairs.add(new BasicNameValuePair("password", "password1"));

        //declare a new HttpClient
        HttpClient httpclient = new DefaultHttpClient();

        //set the HttpPost to the desire URL
        HttpPost httppost = new HttpPost(URL_STRING);

        //set the entity to a new UrlEncodedForm with the login info and type
        httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs, HTTP.UTF_8));

        //store the response
        HttpResponse response = httpclient.execute(httppost);

        //get the data from the response
        String data = new BasicResponseHandler().handleResponse(response);            

        //get the webview from the xml
        WebView webview = (WebView)findViewById(R.id.webView);

        webview.setWebViewClient(new WebViewClient() {  
            @Override  
            public boolean shouldOverrideUrlLoading(WebView view, String url)  
            {  
                view.loadUrl(url);  
                return true;  
            }  
        }); 

        //load the return website from the server
        webview.loadDataWithBaseURL(httppost.getURI().toString(), data, "text/html", HTTP.UTF_8, null);

这成功登录我的URL(HTTPS站点),并打开的页面,但是如果你尝试点击任何网站上它带你回到登录页面中的WebView的按钮,它也不是网站(图表/图形)上显示的许多属性。

This successfully logs me in to the URL (an https site) and opens the page, however if you try to click on any of the buttons on the website it takes you back to the login page in the WebView, and it does not display many of the attributes on the website (charts/graphs).

难道这是一个cookie的事情吗?

Could this be a cookie thing?

那么,有没有办法通过一个新的意图送登录信息?还是有我的WebView实现的解决方案?

So is there a way to send the login info via a new intent? Or is there a solution to my WebView implementation?

(这里是从来没有的应用 Android的SSO(单点登录)比较类似(ISH)问题>)

(here is a relatively similar(ish) question that never got a definitive answer Android SSO (Single sign-on) for app)

感谢您的时间!我真的AP preciate你考虑看看我的问题。

Thank you for your time! I really appreciate you taking a look at my question.

修改:那么giseks的解决方案为我获得网页留在web视图内,但是图表/图形/等的页面仍然没有显示时,该解决方案是简单作为启用JavaScript的WebSettings

EDIT: So giseks's solution worked for me to get the webpage to stay within the WebView, however the charts/graphs/etc on the page still did not display, the solution for that was as simple as enabling javascript for the WebSettings

webview.getSettings().setJavaScriptEnabled(true);

下面是谷歌的Andr​​oid的WebSettings API,以供参考: WebSettings

Here's Google's WebSettings Android API for reference: WebSettings

这帮助了我,我希望它可以帮助你!

This helped me, I hope it helps you!

推荐答案

我的猜测是,你的应用程序不处理cookie的权利。在这个问题看看,它可能会有所帮助。

My guess is that your application doesn't handle cookies right. Take a look at this question, it may help.

的WebView和Cookies在Android

修改

在您的code你似乎只传递从请求到的WebView检索到的HTML。饼干好像迷路的地方。我建议你​​另一种方法。

In your code you seem to pass only the html retrieved from request to the WebView. Cookies seem to get lost somewhere. I'd suggest you another approach.

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

    WebView webv = (WebView)findViewById(R.id.MainActivity_webview);         
    webv.setWebViewClient(new WebViewClient(){
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                view.loadUrl(url);
                return true;
            }
    });

    String postData = FIELD_NAME_LOGIN + "=" + LOGIN +
            "&" + FIELD_NAME_PASSWD + "=" + PASSWD;

    // this line logs you in and you stay logged in
    // I suppose it works this way because in this case WebView handles cookies itself
    webv.postUrl(URL, EncodingUtils.getBytes(postData, "utf-8"));
}

这篇关于通过应用程序通过HttpPost登录到网站的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆