发送的登录与邮政与Android!邮政、Android

2023-09-08 08:35:16 作者:动我姐妹全撂倒

我在Android的,我想用一个HttpPost请求发送到服务器,并得到一个cookie回来的回报,如果登录成功,登录表单。 我有我如何实现一个正确版本的这个问题,我如何才能饼干并将其存储在设备上(我将其存储在preference数据库?这样我就可以摧毁它以后?)。

I have a login form in Android that I want to use to send a HttpPost request to a server and get a cookie back in return if the login was successful. I have one problem how do I implement a correct version of this and how do I get the cookie and store it on the device (do I store it in the preference database? so I can destroy it later on?).

我有这个code现在:

I have this code right now:

public void postData() {
    HttpClient httpclient = new DefaultHttpClient();
    HttpPost httppost = new HttpPost("http://a_site.com/logintest.aspx");

    try {
        List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
        nameValuePairs.add(new BasicNameValuePair("txtUsername", "username"));
        nameValuePairs.add(new BasicNameValuePair("txtPassword", "123456"));
        httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

        HttpResponse response = httpclient.execute(httppost);
        Log.v(TAG, "Response from server: " + response.toString());

    } catch (ClientProtocolException e) {

    } catch (IOException e) {

    }
}

如何获得该cookie以及如何保存它,如果登录成功?

How do I get the cookie and how do I store it if the login was successful?

推荐答案

而不是写你自己的,你可以做最简单的方式:

Rather than writing your own, you can do it the easy way:

inport org.apache.http.util.EntityUtils;
...
HttpResponse response = httpclient.execute(httppost);
String responseAsText = EntityUtils.toString(response.getEntity());