我编写了一个可以正常运行几个月的应用程序,在过去的几天里,我只在已安装的版本上收到了以下错误。
如果我在 VS 中运行源代码,一切正常。此外,bin 文件夹中的 .exe 工作正常。只有安装的版本会产生错误,如果我重新编译并重新安装,我会得到同样的错误。
我对造成这种情况的原因感到有些困惑,并希望得到一些指示。似乎没有返回通过 IE 的 WebRequest 响应,但我很难理解为什么它在 VS 中可以正常工作而没有任何错误。是否有任何新的 IE 安全措施/政策可能导致这种情况?
到目前为止我尝试过的事情包括:
- 禁用所有防病毒和防火墙
- 以管理员身份运行
例外:
Exception: System.Windows.Markup.XamlParseException: The invocation of the constructor on type 'XApp.MainWindow' that matches the specified binding constraints threw an exception. ---> System.Net.WebException: The remote server returned an error: (403) Forbidden.
at System.Net.HttpWebRequest.GetResponse()
at XApp.HtmlRequest.getHtml(Uri uri) in J:\Path\MainWindow.xaml.cs:line 3759
at XApp.MainWindow.GetLinks() in J:\Path\MainWindow.xaml.cs:line 2454
at XApp.MainWindow..ctor() in J:\Path\MainWindow.xaml.cs:line 124
--- End of inner exception stack trace ---
at System.Windows.Markup.WpfXamlLoader.Load(XamlReader xamlReader, IXamlObjectWriterFactory writerFactory, Boolean skipJournaledProperties, Object rootObject, XamlObjectWriterSettings settings, Uri baseUri)
at System.Windows.Markup.WpfXamlLoader.LoadBaml(XamlReader xamlReader, Boolean skipJournaledProperties, Object rootObject, XamlAccessLevel accessLevel, Uri baseUri)
at System.Windows.Markup.XamlReader.LoadBaml(Stream stream, ParserContext parserContext, Object parent, Boolean closeStream)
at System.Windows.Application.LoadBamlStreamWithSyncInfo(Stream stream, ParserContext pc)
at System.Windows.Application.LoadComponent(Uri resourceLocator, Boolean bSkipJournaledProperties)
at System.Windows.Application.DoStartup()
at System.Windows.Application.<.ctor>b__1(Object unused)
at System.Windows.Threading.ExceptionWrapper.InternalRealCall(Delegate callback, Object args, Int32 numArgs)
at MS.Internal.Threading.ExceptionFilterHelper.TryCatchWhen(Object source, Delegate method, Object args, Int32 numArgs, Delegate catchHandler)
Exception: System.Net.WebException: The remote server returned an error: (403) Forbidden.
at System.Net.HttpWebRequest.GetResponse()
at XApp.HtmlRequest.getHtml(Uri uri) in J:\Path\MainWindow.xaml.cs:line 3759
at XApp.MainWindow.GetLinks() in J:\Path\MainWindow.xaml.cs:line 2454
at XApp.MainWindow..ctor() in J:\Path\MainWindow.xaml.cs:line 124
编辑:
这是作为独立应用程序安装的。当我以管理员身份运行时,我打开了程序文件夹并以管理员身份运行 exe 而不是快捷方式。
导致问题的代码是这样的
private void GetLinks()
{
//Navigate to front page to Set cookies
HtmlRequest htmlReq = new HtmlRequest();
OLinks = new Dictionary<string, List<string>>();
string Url = "http://www.somesite.com/somepage";
CookieContainer cookieJar = new CookieContainer();
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Url);
request.CookieContainer = cookieJar;
request.Accept = @"text/html, application/xhtml+xml, */*";
request.Referer = @"http://www.somesite.com/";
request.Headers.Add("Accept-Language", "en-GB");
request.UserAgent = @"Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.2; Trident/6.0)";
request.Host = @"www.somesite.com";
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
String htmlString;
using (var reader = new StreamReader(response.GetResponseStream()))
{
htmlString = reader.ReadToEnd();
}
//More Code
}