在WP8上使用CookieContainer

时间:2014-04-28 17:45:45

标签: c# html cookies windows-phone-8 webclient

我想解析一个html页面。但是,当我在服务器上进行身份验证时,我只能读取此html页面。但是根据我目前的代码,我没有登录。这是我目前的代码:

    // This functions start when I click a button
    private void BtLog_Click(object sender, RoutedEventArgs e)
    {
        string url = "https://subcard.subway.co.uk/de_cardholder/servlet/SPLoginServlet";

        StringBuilder body = new StringBuilder();
        body.Append("language=" + "de");
        body.Append("&user=" + tbUser.text);
        body.Append("&password=" + pbPassword.Password);
        body.Append("&transIdentType=" + "1");
        body.Append("&programID=" + "6");

        bool isNetwork = NetworkInterface.GetIsNetworkAvailable();
        if (!isNetwork)
        {
        }
        else
        {
            try
            {
                WebClient webClient = new WebClient();
                webClient.Headers["Content-Type"] = "application/x-www-form-urlencoded"; // Your Application Header Content-Type 
                webClient.Encoding = Encoding.UTF8;
                webClient.UploadStringAsync(new Uri(url), "POST", body.ToString(), null);

                WebClient client = new WebClient();
                client.DownloadStringCompleted += new DownloadStringCompletedEventHandler(webClient_DownloadStringCompleted);
                client.DownloadStringAsync(new Uri("https://subcard.subway.co.uk/de_cardholder/JSP/SPSummary.jsp"));
            }
            catch
            {
                MessageBox.Show("Error");
            }
        }
    }

    void webClient_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
    {
        MessageBox.Show(e.Result.ToString());
        // If the I am logged in and got the string I want to parse it, but first the other thing have to work
    }

我以为我必须使用cookiecontainer,但我不知道它是如何工作的。感谢您对我的问题的任何建议或解决方案!!

1 个答案:

答案 0 :(得分:0)

根据我的理解。 CookieContainer与您在Web服务器上的会话或身份验证信息无关。

如果您对正在调用的api / Web服务有任何控制权,请尝试从中返回一些令牌信息。比使用您的下一个http请求发送该令牌来验证您的设备。