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