0

我有以下方法从我使用的网站检索数据:

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

如果我保存了我的凭据,该程序可以完美运行。但是,如果我使用新登录,程序会到达这一行并挂起:

WebResponse getResponse = getRequest.GetResponse();

发生超时后,我收到指向此行的 XAML 错误:

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

它说:“PresentationFramework.dll 中发生了‘System.Windows.Markup.XamlParseException’类型的未处理异常”

如果我在保存登录信息的情况下继续重新启动程序,则没有问题。它仍然向站点发出相同的请求以获取数据,我不知道为什么会发生这种情况或如何解决它。

如果有人对如何解决此问题有任何提示或建议,我们将不胜感激。谢谢。


更新:即使从我的 XAML 中完全删除了 OxyPlot,仍然会抛出错误。XAML 本身非常简单:

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

删除 OxyPlot 后,错误指向 MainWindow.xaml 的第 3 行。我没有看到任何其他会导致此类错误的内容。事实上,我可以删除整个网格并仍然得到错误。


更新:我刚刚意识到我错误地配置了 Visual Studio 的异常设置。这是真正的错误:

“在 System.dll 中发生了‘System.Net.WebException’类型的第一次机会异常

附加信息:操作已超时"

指向有问题的原始行:

WebResponse getResponse = getRequest.GetResponse();

就像我说的那样,只有在程序启动时没有保存正确的登录信息时才会发生这种情况。如果我在崩溃后重新启动,假设我在第一次尝试时输入了正确的信息,则没有问题。

更新:看起来这是导致问题的代码:

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

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

在使用 WebRequests、WebResponses 或任何类似性质的东西时,我完全没有经验。什么是在启动时尝试登录的正确方法,然后在初始检查失败时提示输入正确的凭据?我想了解如何以正确的方式做到这一点,以便未来可能更加成功。您能给我的任何建议将不胜感激。

4

2 回答 2

1

更新后,我认为您的问题出在此处:

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

首先,我不确定“while”在这里是否正确,其次,您再次调用 GetWebResponse() 会冻结您之前已经调用过的应用程序(我快速创建的),而 statemenet

于 2013-11-21T21:44:25.060 回答
0

您提供的命名空间(oxyplot)表明问题出在您从该库中使用的控件中。

我看到了基本选项:

1)您可以将完整的项目(oxyplot)直接附加到您的解决方案中,并尝试查看问题可能出在哪里

2)你说当你有数据时,它工作得很好。因此,如果您没有得到响应,很可能某些绑定会导致麻烦。因此,如果您没有得到结果(使用代码、转换器或模板),请尽量避免完全使用该库中的 oxyplot 控件

另外,您的 xaml 在这里可能会更有帮助.....

于 2013-11-21T20:25:01.343 回答