Android工作室:使用cookies

时间:2016-05-28 14:17:07

标签: android cookies httpurlconnection

我目前正在尝试使用Cookie(没有成功),我可以很好地获得它的价值,但发送回来却是一个完全不同的故事。

我使用了两个AsyncTask类,两个按钮和两个文本框。单击第一个按钮后,第一个类访问URL并获取cookie,将其作为字符串值保存到第一个文本框中(正常工作)。 然而,第二次点击应该从文本框中获取cookie并将其发送回第二个URL以显示在线消息(不起作用)。

两个按钮的代码:

    button1.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            //executing the first AsynTask
            new DownloadWebpageTask().execute(FirstLink);
        }
    });

    button2.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            String[] ar= new String[2];
            //save the value of the second link and the cookie value in an array
            ar[0]=SecondLink;
            ar[1]= String.valueOf(textBox1.getText());
            //executing the second AsynTask
            new showC().execute(ar);
        }
    });

AsyncTasks:

//First AsyncTask
private class DownloadWebpageTask extends AsyncTask<String, Void, String> {
    @Override
    protected String doInBackground(String... urls) {
        URL url = null;
        URLConnection conn=null;
        String output="";
        try {
            url = new URL(urls[0]);
            conn = url.openConnection();
            conn.connect();
            return conn.getHeaderField("Set-Cookie");
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return "Error";
    }
    // onPostExecute displays the results of the AsyncTask.
    @Override
    protected void onPostExecute(String result) {
        //store the cookie value as string, something like : PHPSESSID=....;
        textBox1.setText(result);
    }
}


//Second AsyncTask
 private class showC extends AsyncTask<String, Void, String> {
    @Override
    protected String doInBackground(String... urls) {
        URL url = null;
        HttpURLConnection conn=null;
        String output="";
        try {
            url = new URL(urls[0]);
            conn = (HttpURLConnection) url.openConnection();
            conn.setRequestProperty("Set-Cookie", urls[1]);
            conn.connect();
            BufferedReader br = new BufferedReader(new InputStreamReader((conn.getInputStream())));
            output=br.readLine();
            return output+" "+conn.getHeaderField("Set-Cookie");
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return "Error";
    }
    // onPostExecute displays the results of the AsyncTask.
    @Override
    protected void onPostExecute(String result) {
        text2.setText(result);
    }
}
//----------------------

在第二个链接中设置cookie值不起作用,并且仅在再次检查其值时返回不同的cookie。应注意,使用带有Android的cookie的文档很少,大多数文档都基于使用已弃用的HttpClient而不是UrlConnection,我也基于这个example.

的技术

1 个答案:

答案 0 :(得分:1)

在第二个AsyncTask中,在“setRequestProperty”函数中使用“Cookie”而不是“Set-Cookie”。

private class showC extends AsyncTask<String, Void, String> {
@Override
protected String doInBackground(String... urls) {
    URL url = null;
    HttpURLConnection conn=null;
    String output="";
    try {
        url = new URL(urls[0]);
        conn = (HttpURLConnection) url.openConnection();
        // Modification 
        conn.setRequestProperty("Cookie", urls[1]);
        conn.connect();
        BufferedReader br = new BufferedReader(new InputStreamReader((conn.getInputStream())));
        output=br.readLine();
        return output+" "+conn.getHeaderField("Set-Cookie");
    } catch (MalformedURLException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return "Error";
}
// onPostExecute displays the results of the AsyncTask.
@Override
protected void onPostExecute(String result) {
    text2.setText(result);
}
}

上述方法对于国家管理来说非常困难和艰难。您可以使用此link上给出的另一种简单方法。