0

I want to parse a html page. But I can only read this html page when I authenticated on the server. But with my current code, there come's that I am not logged in. Here is my current code:

    // 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
    }

I thought I have to work with cookiecontainer, but I don't know how this works. Thanks for any advice or solutions for my problem!!

  • possible duplicate of [How can I get the WebClient to use Cookies?](http://stackoverflow.com/questions/2825377/how-can-i-get-the-webclient-to-use-cookies) – bdimag Apr 28 '14 at 18:22
  • That could work, but for WP8 I can't use GetWebRequest :( – user3493797 Apr 28 '14 at 18:31

1 Answers1

0

According to my understanding. CookieContainer has nothing to do with your Session or Authentication information on web server.

If you have any control over api/web service that you are calling than try returning some token information from it. Than send that token with your next http request to authenticate your device.

Zeeshan Elahi
  • 247
  • 2
  • 9
  • From my experience, UploadValues is a technically valid way to log in in to sites that use a FORM/POST login. Using the WebClient, if you want it to actually recognize that you're logged in, you need to use cookies, and you can do this be extending the WebClient class to retain and reuse them. – bdimag Apr 28 '14 at 18:27