【问题标题】:Pass Session Cookie in URL Header在 URL 标头中传递会话 Cookie
【发布时间】:2012-03-17 11:35:03
【问题描述】:

我已成功登录并收到 Session Cookie。现在我需要在另一个 URL 的 Header 中传递这个会话 cookie 以获取更多信息。 URL:http://myexperiment.org.uk/whoami.xml 然后应该将自己重定向到另一个生成 XML 树的 URL。我需要使用这个 XML 来获取用户 ID 信息(这是一个树节点)。

这是我的代码。

    Log.d("Session Cookie: ", cookieValue);

    URL urlWhoAmI = new URL("http://www.myexperiment.org/whoami.xml");

    connection1 = (HttpURLConnection) urlWhoAmI.openConnection();
    connection1.setRequestProperty("Set-Cookie", cookieValue);
    connection1.connect();

    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    DocumentBuilder db = dbf.newDocumentBuilder();

    Document doc = db.parse(new InputSource(urlWhoAmI.openStream()));
    doc.getDocumentElement().normalize();

    NodeList nodeList1 = doc.getElementsByTagName("user");

    for(int i=0; i < nodeList1.getLength(); i++)
    {
        Node node = nodeList1.item(i);

        //For user id
        Element firstElement = (Element) node;
        NodeList nameList = firstElement.getElementsByTagName("id");

        Element nameElement = (Element) nameList.item(0);
        nameList = nameElement.getChildNodes();

        String userID = nameList.item(0).getNodeValue();
        Log.d("User ID: ", userID);
    }

当我进行调试时,我得到了 url 的 java.io.FileNotFoundException。

希望有人可以在这里提供帮助。

提前致谢。

【问题讨论】:

    标签: android session-cookies android-xml sessionid http-redirect


    【解决方案1】:

    看起来您正在使用 Set-Cookie,而您应该只使用 Cookie 作为标头字段。 Set-Cookie 用于服务器响应,Cookie 用于客户端请求。

    http://en.wikipedia.org/wiki/List_of_HTTP_header_fields

    【讨论】:

      猜你喜欢
      • 2011-11-02
      • 2016-11-26
      • 1970-01-01
      • 2014-02-18
      • 1970-01-01
      • 1970-01-01
      • 2020-04-22
      • 2021-05-30
      • 2015-02-23
      相关资源
      最近更新 更多