Question

I have the following methods to retrieve data from a website I use:

    private WebResponse GetWebResponse()
    {
        string formUrl = "https://www.draftkings.com/account/login";
        string formParams = string.Format(
            "login={0}&password={1}&returnUrl=",
            Credentials.ToInsecureString(Credentials.DecryptString(Settings.Default.UserName)),
            Credentials.ToInsecureString(Credentials.DecryptString(Settings.Default.Password)));
        WebRequest req = WebRequest.Create(formUrl);
        req.ContentType = "application/x-www-form-urlencoded; charset=UTF-8";
        req.Method = "POST";
        byte[] bytes = Encoding.ASCII.GetBytes(formParams);
        req.ContentLength = bytes.Length;
        using (Stream os = req.GetRequestStream())
        {
            os.Write(bytes, 0, bytes.Length);
        }
        return req.GetResponse();
    }

    private string GetTransactions()
    {            
        var cookieHeader = GetWebResponse().Headers["Set-cookie"];

        while (cookieHeader.StartsWith("u=; domain"))
        {
            var loginWindow = new CredentialView();
            loginWindow.ShowDialog();
            cookieHeader = GetWebResponse().Headers["Set-cookie"];
        }

        string pageSource;
        string getUrl = "https://www.draftkings.com/account/transactions";
        WebRequest getRequest = WebRequest.Create(getUrl);
        getRequest.Headers.Add("Cookie", cookieHeader);
        getRequest.Timeout = 20000;
        WebResponse getResponse = getRequest.GetResponse();
        using (StreamReader sr = new StreamReader(getResponse.GetResponseStream()))
        {
            pageSource = sr.ReadToEnd();
        }            

        return pageSource;
    }

In the event that I have my credentials saved, the program works perfectly. However, if I am using a fresh login, the program gets to this line and hangs:

WebResponse getResponse = getRequest.GetResponse();

After the timeout occurs, I get a XAML error pointing at this line:

xmlns:oxy="http://oxyplot.codeplex.com"

It says: "An unhandled exception of type 'System.Windows.Markup.XamlParseException' occurred in PresentationFramework.dll"

If I then proceed to restart the program with my login info saved, there are no problems. It still makes the same requests to the site to grab the data and I have no idea why this is happening or how to fix it.

If anybody has any tips or advice on how to fix this, any help would be greatly appreciated. Thanks.


Update: Even with OxyPlot entirely removed from my XAML, the error is still thrown. The XAML itself is very simple:

<Window x:Class="DraftKingsTracker.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:oxy="http://oxyplot.codeplex.com"
        Title="DraftKings Tracker" Height="768" Width="1024">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto" />
            <RowDefinition Height="*" />
        </Grid.RowDefinitions>
        <Button Content="Account" 
                Command="{Binding CommandAccount}"
                HorizontalAlignment="Left"
                Margin="5" />
        <oxy:Plot Grid.Row="1"
                  Model="{Binding PlotModel}"
                  Margin="10" />
    </Grid>
</Window>

With OxyPlot removed, the error points to line 3 of MainWindow.xaml. I don't see anything else in there that would cause such an error. In fact, I can remove the entire grid and still get the error.


Update: I just realized that I had Visual Studio's exception settings configured incorrectly. Here's the real error:

"A first chance exception of type 'System.Net.WebException' occurred in System.dll

Additional information: The operation has timed out"

Pointing at the original line in question:

WebResponse getResponse = getRequest.GetResponse();

Like I said, this only happens if the program is launched without proper login info saved. If I restart after it crashes, assuming I entered the correct information on the first attempt, there are no issues.

Update: It looks like this is the code that's causing problems:

    var cookieHeader = GetWebResponse().Headers["Set-cookie"];

    while (cookieHeader.StartsWith("u=; domain"))
    {
        var loginWindow = new CredentialView();
        loginWindow.ShowDialog();
        cookieHeader = GetWebResponse().Headers["Set-cookie"];
    }

I have no experience at all when it comes to using WebRequests, WebResponses, or anything of that nature. What would be the proper way to attempt to login at startup, and then prompt for proper credentials if the initial check fails? I would like to learn how do this the right way so that a may be more successful in the future. Any advice you could give me would really be appreciated.

Was it helpful?

Solution

after your update i think your problem resides here :

while (cookieHeader.StartsWith("u=; domain"))
    {
        var loginWindow = new CredentialView();
        loginWindow.ShowDialog();
        cookieHeader = GetWebResponse().Headers["Set-cookie"];
    }

first of all I am not sure 'while' is a correct thing here, second, you call GetWebResponse() again which freezes the app ( which i quick-created ) you already called it before while statemenet

OTHER TIPS

namespace you provided (oxyplot) suggests the problem is in controls you use from that library.

I see tho basic options:

1) You can either attach a complete project directly (oxyplot) to your solution and try to see where the problem may be

2) you say that when you have the data, it works well. So most likely some binding is causing the trouble if you dont get the response. Therefore try to avoid using the controls from oxyplot from that library altogether if you dont get the result ( use code, converter or templating)

also, your xaml would be probably more helpful here .....

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top