Aylardır sorunsuz çalışan bir uygulama yazdım, son birkaç gündür sadece yüklü sürümde aşağıdaki hatayı alıyorum.
Kaynak kodu VS'de çalıştırırsam her şey düzgün çalışıyor. Ayrıca, bin klasörlerindeki .exe'ler de sorunsuz çalışıyor. Hatayı oluşturan sadece yüklü sürüm, yeniden derleyip yeniden yüklersem aynı hatayı alıyorum.
Buna neyin neden olduğu konusunda biraz şaşkınım ve birkaç ipucu bulmayı umuyorum. Görünüşe göre IE üzerinden bir WebRequest yanıtı döndürülmüyor, ancak VS'de herhangi bir hata olmadan neden iyi çalıştığını anlayamıyorum. Buna neden olabilecek yeni IE güvenlik önlemleri/politikaları var mı?
Şimdiye kadar denediklerim arasında şunlar var:
İstisna:
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
DÜZENLE:
Bu bağımsız bir uygulama olarak yüklendi. Yönetici olarak çalıştırdığımda, program klasörünü açtım ve kısayol yerine exe'yi yönetici olarak çalıştırdım.
Soruna neden olan kod şudur
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
}
Aşağıdaki satırı ekleyin:
request.UseDefaultCredentials = true;
Bu, uygulamanın siteye erişmek için oturum açmış kullanıcının kimlik bilgilerini kullanmasına izin verecektir. Eğer 403 döndürüyorsa, açıkça kimlik doğrulaması bekliyor demektir.
Ayrıca (şimdi?) uzak site ile aranızda bir kimlik doğrulama proxy'si olması da mümkündür. Bu durumda, deneyin:
request.Proxy.Credentials = System.Net.CredentialCache.DefaultCredentials;
Umarım bu yardımcı olur.
Sorun sunucu tarafında gibi görünüyor.
Benim durumumda paypal sunucusu ile çalıştım ve önerilen cevapların hiçbiri yardımcı olmadı, ancak http://forums.iis.net/t/1217360.aspx?HTTP+403+Forbidden+error
Bu sorunla karşı karşıyaydım ve az önce Paypal teknik ekibinden cevap aldım. Bunu eklemek 403 sorununu çözecektir.
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
req.UserAgent = "[5 karakterden fazla olan herhangi bir kelime]";
Bu bir izin hatasıdır. VS'niz muhtemelen yükseltilmiş bir hesap veya yüklü sürümü kullanan kullanıcıdan farklı bir kullanıcı hesabı kullanarak çalışır.
IIS izinlerinizi kontrol etmeniz ve eriştiğiniz kaynağa hangi hesapların erişimi olduğunu görmeniz faydalı olabilir. Kullandığınız hesap ve yüklü sürümlerin kullandığı hesap ile çapraz referans alın.