我写了一个应用程序,几个月来一直运行良好,在过去的几天里,我只在安装的版本上得到了以下错误。
如果我在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
EDIT:
这是作为一个独立的应用程序安装的。当我以管理员身份运行时,我打开程序文件夹,以管理员身份运行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
}
看起来问题是基于服务器端的。
在我的案例中,我使用了支付宝服务器,建议的答案都没有帮助,但是http://forums.iis.net/t/1217360.aspx?HTTP+403+Forbidden+error
我正面临这个问题,刚刚得到Paypal技术部的答复。 添加这个可以解决403的问题。
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
<br>;req.UserAgent = "[任何超过5个字符的词]";
。